2017-02-16 28 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] –

相關問題