gpt4 book ai didi

objective-c - Objective-C : How to reload data in parent cell from subclass

转载 作者:行者123 更新时间:2023-11-28 23:15:17 25 4
gpt4 key购买 nike

我有一个 UITableView,每个单元格都包含以下内容

1) 一个 UILabel(列出点击按钮的人的名字)

2) 一个按钮(单击时会将答题器的名称添加到 UILabel 中显示的列表中)

按钮是 UIControl 子类的一个实例。所以意图是当按钮被点击时,我将当前用户添加到UILabel使用的数组中进行显示。

但是,我不确定如何重新加载父单元格(其中包含按钮)

只是重申我的问题

1) 向列表中添加用户名是在按钮子类中完成的,而不是在 UITableViewCell 中完成的,更新数组后如何调用父单元格?

2) 我不想重新加载整个表格,只是重新加载用户单击按钮的特定单元格。

提前致谢

最佳答案

在作为 TableView 数据源的 View Controller 中编写一个操作方法,例如:

-(IBAction)reloadCell:(id)theCell {
NSIndexPath *idxPath = [theTableView indexPathForCell:theCell];
[theTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:idxPath, nil]
withRowAnimation:YES]; // or NO ;)
}

然后在 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 方法中,在返回单元格之前,像这样配置它:

// ...
[cell.theButton addTarget:self
action:@selector(reloadCell:)
forControlEvents:UIControlEventTouchUpInside];
return cell;

这将使 -(IBAction)reloadCell:(id)sender; 方法在触摸按钮并重新加载特定单元格时调用,但此解决方案可能会让您移动更新的代码列表的内容,因为我不知道先发送哪条消息(更新内容的消息,还是重新加载单元格的消息)。

所以我认为最好的方法是,让您的自定义按钮子类对 Controller 有一个弱引用,这样按钮就可以更新内容,然后向 Controller 发送消息,并且基于相同的操作工作 Controller 中的方法。

在你的按钮子类.h中

YourControllerType *tableController;

@property(assign) YourControllerType *tableController;

在您的按钮子类 .m 中

@synthesize tableController

// in the method where you update the list
[tableController reloadCell:self.superview];

也不要忘记正确配置单元格

// ...
cell.theButton.tableController = self;
return cell;

关于objective-c - Objective-C : How to reload data in parent cell from subclass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6724346/

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