2012-06-25 75 views
2

我使用自定義的複選框按鈕在我的表視圖,並希望所選單元格的數據保存到一個可變數組....誰能幫我的tableview細胞的數據...謝謝如何保存

+0

你已經擁有的數據,你只需要得到具體的數據,並存儲到另一個可變陣列,我說得對嗎? – janusbalatbat

+0

張貼我的答案... :) – janusbalatbat

回答

3

創建一個可變數組來存儲您選擇的數據,讓我們把它叫做「yourSeparatedData」,設置的cellForRowAtIndexPath您的複選框的標籤,並設置的oncheck:方法的目標。代碼如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSString *CellIdentifier = @"setMe"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyle……… 
    } 
    checkBox = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    checkBox.frame = CGRectMake(customizeMe); 
    if(yourSeparatedData && [yourSeparatedData indexOfObject:[yourTableViewDataSource objectAtIndex:indexPath.row]] != NSNotFound) 
    { 
     [checkBox setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal]; 
    } 
     else { 
     [checkBox setBackgroundImage:[UIImage imageNamed:@"unCheck.png"] forState:UIControlStateNormal]; 
     } 
     [checkBox addTarget:self action:@selector(onCheck:) forControlEvents:UIControlEventTouchUpInside]; 
     [checkBox setTag:indexPath.row]; 

     [cell addSubview:checkBox]; 
     return cell; 

}

-(void)onCheck:(id)sender { 
    if(yourSeparatedData && [yourSeparatedData indexOfObject:[yourTableViewDataSource objectAtIndex:[sender tag]]] != NSNotFound) 
     { 
     [sender setBackgroundImage:[UIImage imageNamed:@"unCheck.png"] forState:UIControlStateNormal]; 
     [yourSeparatedData removeObject:[yourTableViewDataSource objectAtIndex:[sender tag]]]; 
     } 
     else { 
     [sender setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal]; 
     [yourSeparatedData addObject:[yourTableViewDataSource objectAtIndex:[sender tag]]]; 
     } 
     [yourTableView reloadData]; 
} 

這個代碼不進行測試,你正在使用複選框,所以我認爲你要的不只是一個數據分開,在的結束選擇,你將有'yourSeparatedData'與從你的tableView中挑選的對象。

+0

歡呼聲....你的代碼幫了我很多。 –

+0

歡迎您:) – janusbalatbat

0

你有要手動完成此操作,UITableView或UITableViewController中沒有任何設施可以自動執行此操作。

0

當用戶選擇任何單元格,然後在didSelectRowAtIndexPath中,您可以動態添加所選對象。

[someMutableArr addObject:[tableArr objectAtIndex:indexPath.row]]; 
+0

我使用自定義按鈕...所以我不能通過點擊行將它們添加到數組中...我該怎麼做? –

0

試試這個

- (void)onButtonClick { 

    int numberOfSections = [tableView numberOfSections]; 

    for (int section = 0; section < numberOfSections; section++) { 

     int numberOfRows = [tableView numberOfRowsInSection:section]; 

     for (int row = 0; row < numberOfRows; row++) { 

      NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section]; 
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

      if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { 

       // Cell is selected 
       //here you can add that values into an array 
      } else { 

       // Cell is not selected 
      } 
     } 
    } 
} 
+0

如何將這些選定的行保存在數組中? –

1

你可以嘗試自定義操作中的UITableView單元格的按鈕prees,你也可以使用放一個複選框圖片和點擊就可以改變形象,在這裏檢查是代碼

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{ 
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"identifire"]; 
cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifire"] autorelease]; 
    cell.detailTextLabel.text=[id_Array objectAtIndex:indexPath.row]; 
    cell.detailTextLabel.hidden=YES; 
    button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"n_cross" ofType:@"png"]; 
    UIImage *buttonImage1 = [[UIImage alloc] initWithContentsOfFile:path1]; 
    [button setImage:buttonImage1 forState:UIControlStateNormal]; 
    [button addTarget:self 
       action:@selector(customActionPressed:) 
    forControlEvents:UIControlEventTouchDown]; 
    [button setTitle:@"Custom Action" forState:UIControlStateNormal]; 
    button.frame = CGRectMake(245.0f, 20.0f, 40.0f, 40.0f); 
    [cell addSubview:button]; 
    [buttonImage1 release]; 
     CGRect imageFrame=CGRectMake(10,8,50,50); 
     self.cellimage=[[[UIImageView alloc] initWithFrame:imageFrame] autorelease]; 
     self.cellimage.image=[imageIdArray objectAtIndex:indexPath.row]; 
     [cell.contentView addSubview:self.cellimage]; 
     return cell; 
} 
-(void)customActionPressed :(id)sender 
{ 
//Get the superview from this button which will be our cell 
UITableViewCell *owningCell = (UITableViewCell*)[sender superview]; 
NSIndexPath *cell = [_tableView indexPathForCell:owningCell]; 
NSString *uid=[id_Array objectAtIndex:cell.row]; 
[id_Array removeObjectAtIndex:cell.row]; 
[_tableView reloadData]; 
[self performSelectorInBackground:@selector(ignoreRequest:) withObject:uid]; 
} 

這裏我有一個id_Array並在單元的選擇我只是刪除對象索引