gpt4 book ai didi

ios - UITableViewCell 如何与其 UITableView 通信?

转载 作者:可可西里 更新时间:2023-11-01 03:34:40 25 4
gpt4 key购买 nike

我目前正在创建一个自定义 GridView ,这意味着我正在创建一个与 UITableView 有很多共同点的类。我想要做对的一件事是单元格和 GridView 的通信。

因此我想知道 TableView 单元格如何与其 TableView 对话。例如,单元格如何通知 TableView 它的删除按钮被点击并且单元格需要从 TableView 中删除?

有几种可能的情况,但我不确定 Apple 使用的是哪一种,因为 UITableViewUITableViewCell 的 header 揭示了这一点(或者我忽略了什么).

最终,目标是让单元格和 GridView 私下通信,即不公开任何公共(public)方法或协议(protocol)(如果可能的话)。

最佳答案

现在删除按钮可能不是一个很好的例子,因为 iOS 有一个内置方法,允许您删除行并通知您的数据源调用:

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

但是,为了便于理解,如果您想向 tableview 单元格添加一个按钮并让它执行标准 iOS 库中没有的操作,您可以在单元格中创建一个委托(delegate)并设置 tableview 的数据源文件作为代表。

基本上你会像这样子类化 UITableViewCell

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

我的数据源.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,您只需创建一个委托(delegate)方法,告诉用户按下了某个按钮。

关于ios - UITableViewCell 如何与其 UITableView 通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10814757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com