繁体   English   中英

从UITableViewCell向数组添加对象

[英]Add objects into array from UITableViewCell

在这里我需要为多个行选择实现将对象添加到数组中,这是我的代码

BOOL selectedRow

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath{

if(selectedRow){
 UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
            cellText = selectedCell.textLabel.text;
            NSLog(@"Airline cellTextC = %@",cellText);

if ([cellText isEqualToString:NameString]) {
                NSLog(@"Selected NameString Index = %@",indexes);

                [nameArray addObject:indexes];

            }else{
                NSLog(@"Unknown Selected Name");
            }

}
NSLog(@"Total Names  = %@",nameArray.description);

}

上面的代码是一个节和多行,如果选择了一个行name string应该添加到Array它仅在选择一行时有效。但是我需要从多个行选择中添加对象。能否请你建议我谢谢你。

您可能想要更新表视图的数据源,并让表视图数据源方法为您更新表视图。

检查是否可以解决您的问题:

[tableView setAllowsMultipleSelection:YES];

如果要选择多行并将对象添加到数组中,建议创建2个NSMutableArray ,第一个包含所有数据,另一个包含选定的行数据。

@property (strong , nonatomic) NSMutableArray *myalldatatab;
@property (strong , nonatomic) NSMutableArray *selecteddata;

然后使用didselectrowsatindexpath和didDeselectRowAtIndexPath更改标记

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}

您可以通过调用以下行来获取所选行的索引:

NSArray *selectedCells = [self.tableView indexPathsForSelectedRows];

您现在可以迭代选定的单元格并添加这样的对象

for (int i = 0; i < [selectedCells count]; i++)
        {
[self.selecteddata addObject: [selectedCells objectAtIndex:i]]
}

希望它能对您有所帮助:))快乐!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM