自定义UITableViewCell知道什么单元格被修改了一个checkbox

我有一个自定义单元格有一个checkbox,

一切工作正常,checkbox得到检查根据我传递给我的子类UITableViewCell的字典,

但现在我需要传递给具有表格视图的类的确切单元格,我的checkbox被修改,所以我可以设置我的可变字典与特定单元格的新选中或未选中状态,

那么如何做到这一点,我应该使用一个委托?这是好的,但问题是,我怎么知道我的checkbox被修改在什么单元?

你可以使用这样的委托…

MyCell.h

@protocol MyCellDelegate <NSObject> -(void)cellCheckBoxWasChanged:(MyCell *)cell; @end @interface MyCell : NSObject @property (nonatomic, weak) id <MyCellDelegate> delegate; @end 

MyCell.m

 @implementation MyCell - (void)checkBoxChanged { [self.delegate cellCheckBoxWasChanged:self]; } @end 

然后find你可以做的索引…

TableViewController.m

 - (void)cellCheckBoxWasChanged:(MyCell *)cell { NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; // do something to your array. } 

你为什么不把自己的UITableViewCell也作为委托方法传递给self

所以对于那个单元格,你可以得到indexpath

 NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; 

根据cellForRowAtIndexPath:方法中的单元格Indexpath设置每个checkbox的标记值。

  UIButton *checkboxes = customCell.checkButton [checkboxes setTag:indexPath.row]; 

然后在button的操作方法。

检查发件人。标签值以获得按下button的确切行

 UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]]; 

你可以这样做 –

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; if (cell == nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } if([[[Usercontacts objectAtIndex:indexPath.row]objectForKey:@"isChecked"]isEqualToString:@"NO"]) { [checkUncheckBtn setImage:[UIImage imageNamed:@"unchecked box.png" ]forState:UIControlStateNormal]; checkUncheckBtn.tag=1; } else { [checkUncheckBtn setImage:[UIImage imageNamed:@"checked box.png" ]forState:UIControlStateNormal]; checkUncheckBtn.tag=2; } [checkUncheckBtn addTarget:self action:@selector(checkUncheckBtnPressed:) forControlEvents:UIControlEventTouchUpInside]; return cell; } 

当你执行checkUncheckBtnPressed :方法它看起来像

 -(void)checkUncheckBtnPressed:(id)sender { UIButton *btn=(UIButton*)sender; UITableViewCell *cell =(UITableViewCell *) [sender superview] ; NSIndexPath *_indxpath = [createGroupContactsTableView indexPathForCell:cell]; if(btn.tag==1) { [btn setImage:[UIImage imageNamed:@"checked box.png" ]forState:UIControlStateNormal]; btn.tag=2; [[Usercontacts objectAtIndex:_indxpath.row]setObject:@"YES" forKey:@"isChecked"]; } else { [btn setImage:[UIImage imageNamed:@"unchecked box.png" ]forState:UIControlStateNormal]; btn.tag=1; [[Usercontacts objectAtIndex:_indxpath.row]setObject:@"NO" forKey:@"isChecked"]; } } 

这里有一个替代scheme,让单元监听来自checkbox的事件,并使用委托模式将它们转发到UITableViewController:

让UITableViewController监听CheckBox中的事件并使用下面的代码来确定单元的NSIndexPath:

 @implementation UITableView (MyCategory) -(NSIndexPath*)indexPathOfCellComponent:(UIView*)component { if([component isDescendantOfView:self] && component != self) { CGPoint point = [component.superview convertPoint:component.center toView:self]; return [self indexPathForRowAtPoint:point]; } else { return nil; } } @end