UITableViewCell如何与其UITableView进行通信?

我目前正在创build一个自定义的网格视图,这意味着我正在创build一个与UITableView有很多相同的类。 我想要得到正确的事情之一是单元格和网格视图的沟通。

因此,我想知道表格视图单元如何与其表格视图进行交谈。 例如,单元格如何通知表视图,它的删除button已被点击,并且单元格需要从表格视图中删除?

有几种可能的情况,但我不知道哪一个被苹果使用,因为UITableViewUITableViewCell的头文件揭示了这个(或者我忽略了一些东西)。

最终,目标是让单元格和网格视图进行私密通信,即不暴露任何公共方法或协议(如果这是可能的话)。

现在删除button可能是一个不好的例子,因为iOS有一个内置的方法,允许你删除行并通知你的数据源叫:

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

但是,为了理解如果你想添加一个button到你的tableview单元格并让它执行一个不在标准的iOS库中的操作,你可以在你的单元格中创build一个委托并将你的tableview的数据源文件设置为委托。

基本上你会像这样inheritanceUITableViewCell

MyCustomCell.h

 @protocol MyCustomCellDelegate; @interface MyCustomCell : UITableViewCell @property (nonatomic, unsafe_unretained) id <MyCustomCellDelegate> delegate; //Holds a reference to our tableView class so we can call to it. @property (nonatomic, retain) NSIndexPath *indexPath; //Holds the indexPath of the cell so we know what cell had their delete button pressed @end /* Every class that has <MyCustomCellDelegate> in their .h must have these methods in them */ @protocol MyCustomCellDelegate <NSObject> - (void)didTapDeleteButton:(MyCustomCell *)cell; @end 

MyCustomCell.m

 @synthesize delegate = _delegate; @synthesize indexPath = _indexPath; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { /* Create a button and make it call to a method in THIS class called deleteButtonTapped */ UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(5, 5, 25, 25); [button addTarget:self action:@selector(deleteButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; } return self; } /** * This is the method that is called when the button is clicked. * All it does is call to the delegate. (Whatever class we assigned to the 'delegate' property) */ - (void)deleteButtonTapped:(id)sender { [self.delegate didTapDeleteButton:self]; } 

你的TableView的数据源看起来像这样。

MyDataSource.h

 /* We conform to the delegate. Which basically means "Hey you know those methods that we defined in that @protocol I've got them and you can safely call to them" */ @interface MyDataSource : UIViewController <MyCustomCellDelegate, UITableViewDelegate, UITableViewDataSource> @property (nonatomic,retain) NSArray *tableData;//We will pretend this is the table data @property (nonatomic,retain) UITableView *tableView;// We will pretend this is the tableview @end 

MyDataSource.m

 //We will pretend we synthesized and initialized the properties - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier: @"MyCustomCell"]; if (!cell) cell = [[DownloadQueueCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @"MyCustomCell"]; cell.delegate = self; // Make sure we set the cell delegate property to this file so that it calls to this file when the button is pressed. cell.indexPath = indexPath;// Set the indexPath for later use so we know what row had it's button pressed. return cell; } - (void)didTapDeleteButton:(MyCustomCell *)cell; { // From here we would likely call to the apple API to Delete a row cleanly and animated // However, since this example is ignoring the fact that they exist // We will remove the object from the tableData array and reload the data [self.tableData removeObjectAtIndexPath:cell.indexPath]; [self.tableView reloadData]; } 

基本上,长话短说。 对于你的gridview,你只需要创build一个委托方法来告诉用户某个button被按下了。

UITableViewCell项目是UITableViewCell的子视图。 所以你可以用它来在单元格和tableView之间进行通信。 依次UITableView有委托和数据源来与其控制器进行通信。 这可能有帮助。

我不确定是否需要私人通讯频道。

表视图通过调整表视图单元格的大小并在开放空间中创build一个新视图来施加与给定单元格相邻的删除视图。

强加的删除视图通过表视图,索引path和表视图委托实例化。 删除视图处理触摸并将消息发送到包含表视图和索引path的表视图委托。 表视图委托做从数据源中删除条目的工作,animation删除单元格和刷新表视图。 刷新后,表视图根据数据源重绘所有可见单元格。

我不确定这是否是你正在寻找,但看看这个问题: 私人扩展一个协议使用目标c中的类别

你可以让你的自定义单元格UIViews有一个你的网格视图types的私有属性。 将这些单元格添加到GridView时,将该属性更新到gridView。

我有我的自定义网格,这样做。

另一种方法是在你的网格中传递一个单元格的方法,然后返回索引。 UITableView也有这些方法。 这样当一个单元格中的button被按下时,你所要做的就是获取单元格并传递给网格,这将返回一个索引。 用那个索引你访问数据…

您可以使用类别 。

您将您的私有方法声明在一个单独的类别中,并将其放置到单独的文件中。 在想要使用这些私有方法的类的实现文件中,使用私有类别导入该文件,并使用私有方法。 所以使用它们的类的公共.h被保持不变。

例:

MyGridViewCell.h:

 @interface MyGridViewCell : UIView // ... @end 

MyGridViewCell.m:

 @implementation MyGridViewCell : UIView // ... @end 

现在私人方法分类界面:

MyGridViewCellPrivate.h:

 @interface MyGridViewCell (Private) - (void) privateMethod1; @end 

并执行:

MyGridViewCellPrivate.m:

 @implementation MyGridViewCell (Private) - (void) privateMethod1 { // ... } @end 

标题与以前一样:

MyGridView.h:

 @interface MyGridView : UIView - (void) publicMethod1; @end 

但是实现可能使用私有API:

MyGridView.m:

 #import "MyGridViewCell.h" #import "MyGridViewCellPrivate.h" - (void) publicMethod1 { // Use privateMethod1 }