如何从uitableview中获取多个选定的行值并在数组中保存多个选定的值?

时间:2017-02-16 09:41:56

标签: ios objective-c uitableview

我在UITableView.Now中有状态列表我想选择UITableView的多行,并想在一个数组中获取这个选定的行值。我可以得到这个吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSMutableArray *arrData =[statearray objectAtIndex:indexPath.row];
    NSLog(@"arrdata>>%@",statearray);

    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [statearray objectAtIndex:indexPath.row];
    return cell;
}


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

    [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

}

4 个答案:

答案 0 :(得分:2)

您可以致电tableview.indexPathForSelectedRows。这将输出一个数组,其中包含tableview中所有选定行的索引路径!

这是你的代码在swift 3中的样子:

var values : [String] = []
let selected_indexPaths = tableView.indexPathsForSelectedRows
for indexPath in selected_indexPaths! {
    let cell = tableView.cellForRow(at: indexPath)
    values.append((cell?.textLabel?.text)!)
}

运行此代码后,所有选定单元格的值应位于values数组中。

答案 1 :(得分:2)

您可以通过以下方式跟踪所选单元格

var selectedCells: [UITableViewCell] = []
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedCells.append(tableView.cellForRow(at: indexPath)!)
}

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let deselectedRow = tableView.cellForRow(at: indexPath)
    if(selectedCells.contains(deselectedRow!)){
        let indx = selectedCells.index(of: deselectedRow!)
        selectedCells.remove(at: indx!)
    }
}

想法是在发生单元格选择时维护所选单元格的数组,并在取消选择后删除单元格

最好的方法是使用方法

获取选定的索引路径
tableView.indexPathsForSelectedRows

您可以在数组中获取数据

var oldArray: [NSIndexPath] = [NSIndexPath.init(row: 0, section: 0), NSIndexPath.init(row: 1, section: 0), NSIndexPath.init(row: 2, section: 0), NSIndexPath.init(row: 3, section: 0)]
var newArray: [Int] = oldArray.flatMap { ($0.row) }

就像我们有oldArray形式的数组一样,我们只能使用flatMap获取行。

答案 2 :(得分:1)

有tableview的默认方法, self.tableView.indexPathsForSelectedRows

它为您提供了一系列选定的索引路径。但是你还需要设置tableview的属性

self.tableView.allowsMultipleSelection = true

答案 3 :(得分:0)

如果想要追回选择的行,

NSArray *selectedRowsArray = [yourTableViewName indexPathsForSelectedRows];

回答更新

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

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    //the below code will allow multiple selection
    if ([yourMutableArray containsObject:indexPath])
    {
       [yourMutableArray removeObject:indexPath];
    }
    else
    {  
        [yourMutableArray addObject:indexPath];

    }
    [yourTableView reloadData];

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = @"SimpleTableItem";


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    if ([yourMutableArray containsObject:indexPath])

    {

       //Do here what you wants

       if (contentArray.count > 0) {

            NSDictionary *dictObje = [contentArray objectAtIndex:indexPath.row];
            cell.textLabel.text = [NSString stringWithFormat:@"%@",[dictObje objectForKey:@"name"]];

       }

        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {

       //Do here what you wants
        if (contentArray.count > 0) {

            NSDictionary *dictObje = [contentArray objectAtIndex:indexPath.row];
            cell.textLabel.text = [NSString stringWithFormat:@"%@",[dictObje objectForKey:@"name"]];

       }

        cell.accessoryType = UITableViewCellAccessoryNone;

    }

return cell;
}