2017-02-16 70 views
1

我有UITableView.Now中的状态列表我想选择UITableView的多行并希望在一个数组中获得此选定的行值。我可以得到它吗?如何从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; 

} 
+0

进入转移tableView编辑模式,然后保存选择状态 – Allen

回答

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阵列后。

+0

UITableViewCell * selectedCell = [tableView cellForRowAtIndexPath:indexPath]; NSString * cellText = selectedCell.textLabel.text; – sp309

+0

通过这种方式,我得到的选择行值string.but如何获得多个选定的行值与一个数组与逗号seperater? – sp309

+0

你可以使用'tableView.indexPathForSelectedRows'来给你一个索引路径数组。然后你可以创建一个'for循环'并循环遍历这个数组中的所有索引路径,并像你在代码中一样获取每个索引路径的单元格,并将结果保存在一个数组中。 –

0

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

,让你选择indexpaths的数组。但你还需要设置的tableview

self.tableView.allowsMultipleSelection = true

0

的财产,如果想回selcted行跟踪,

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; 
} 
+0

我想选择所有的行值作为array.how的对象我可以得到? – sp309

+0

朋友你是什么意思选择所有行的值? –

+0

@ sp309为什么您要为选定行选择数组结构? 'tableView.indexPathForSelectedRows'是数组选择的行,你可以更详细地说明问题吗? – ystack

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 

你可以得到的数据列于做

最好的办法是让选择indexpaths一个数组

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得到行。

+0

与tableView.indexPathsForSelectedRows我会得到的部分和行的数组我如何才能获得行数组? –

+0

部分和行的组合使数据具有唯一性,因此如果只需要行,那么可能会在不同部分中选择相同的行号,这可能会变得模糊不清。如果您可以提供详细信息,您希望如何获得数据。我可能会帮助你。 –

+0

我只想选择行索引,但我会收到一些像这样[[0,0],[0,1],[0,4]]的东西,因为您会看到我将收到行的索引但我将收到索引部分但我不想要它!我想得到一个像这样的选定行索引[0,1,4] –

相关问题