2011-05-03 38 views
1

我正在將一個複選框放在表格視圖的每一行中。當我點擊一行時,它的值應該保存到一個數組中,在另一行的抽頭中,它的值也應該保存到同一個數組中,並且在同一行的一個抽頭中,該值應該從數組中刪除。在表格視圖的同一行上多次敲擊

請給我建議如何實現這一個。我在didSelectRowAtIndexPath方法中使用了以下代碼,但我無法做到這一點。

if([arrData count]==0) 
{ 
    strlast = [arrName objectAtIndex:indexPath.row]; 
    [arrData addObject:strlast]; 
    NSLog(@"string checked in arrData %@",strlast); 
} 
else 
{ 
    for(int i = 0 ;i < [arrData count]; i++) 
    { 
     NSLog(@"[arrData count]:%d",[arrData count]); 
     strSelected = [arrName objectAtIndex:indexPath.row]; 
     NSLog(@"strSelected:%@",strSelected); 

     for(int i = 0 ;i < [arrData count]; i++) 
     { 
      if([strSelected caseInsensitiveCompare:[arrData objectAtIndex:i]]) 
      { 
       [arrData addObject:strSelected]; 
       NSLog(@"arrData:%@",arrData); 
      } 
     } 
    } 
} 

回答

6

列表是一個包含與自己的數組名 假設tableArray是您的數組中值插入和刪除的所有在tableview中查看的數據替換它的數組名。 在視圖h文件

NSMutableArray *tableArray; 

在.m文件didload

tableArray=[[NSMutableArray alloc]init]; 

的tableview didselect行方法: -

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




if ([tableArray count]>0) { 

     if ([tableArray containsObject:[list objectAtIndex:indexPath.row]]) { 
      [tableArray removeObject:[list objectAtIndex:indexPath.row]]; 
      NSLog(@"data removed"); 
      NSLog(@"tableArray%@",tableArray); 
     } 
     else { 
      [tableArray addObject:[list objectAtIndex:indexPath.row]]; 
      NSLog(@"data added"); 
      NSLog(@"tableArray%@",tableArray); 
     } 

    } 
    else { 
     //[tableArray addObject:[NSString stringWithFormat:@"%d",indexPath.row]]; 
     [tableArray addObject:[list objectAtIndex:indexPath.row]]; 
     NSLog(@"data added"); 
     NSLog(@"tableArray%@",tableArray); 
    } 


} 

釋放的dealloc 陣列我已經測試的代碼我希望它可以幫助你......

+0

@Jenifer:邏輯是對的,但是,它返回行數字不是該行上的項目。即我有四個四行的名字,我想要得到他們的操作 – sujay 2011-05-03 05:01:59

+2

所以如果你得到的rowno,那麼你可以找到該行的項目沒有,它非常簡單 – Gypsa 2011-05-03 05:04:29

+0

好吧,我明白看到編輯答案它會給你行中的項目不是行號。 – Gypsa 2011-05-03 05:08:02

相關問題