Objective C:如何从子类重新加载父单元格中的数据
我有一个 UITableView,每个单元格包含以下内容
1)一个 UILabel (列出单击该按钮的人员的姓名)
2)一个按钮(单击时会将点击者的名称添加到 UILabel 中显示的列表中)
该按钮是一个实例UIControl 的子类。所以意图是当按钮被点击时,我将当前用户添加到 UILabel 使用的数组中进行显示。
但是,我不确定如何重新加载父单元格(其中包含按钮)
只是为了重申我的问题
1)将用户名添加到列表中是在按钮子类中完成的,而不是 UITableViewCell 中,我该如何更新数组后调用父单元格?
2)我不想重新加载整个表格,只想重新加载用户单击按钮的特定单元格。
提前致谢
I have a UITableView with each cell containing the following
1) A UILabel (listing the names of people who clicked on the button)
2) A button (when clicked will add the clicker's name to the list displayed in UILabel)
The button is an instance of a subclass of UIControl. So the intention is that when the button is clicked, I will add the current user to the array used by the UILabel for display.
However, I am unsure how I can reload the parent cell (in which the button is contained)
Just to re-iterate my issue
1) The addition of user name to list is done in the button subclass and not the UITableViewCell, how can I call the parent cell once I have updated the array?
2) I do not want to reload the entire table, just the specific cell in which the user has clicked the button.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在作为表视图数据源的视图控制器中编写一个操作方法,例如:
然后在
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
方法中,之前返回单元格,像这样配置它:这将使
-(IBAction)reloadCell:(id)sender;
方法在触摸按钮时调用,并重新加载特定的单元格,但此解决方案可能会让您移动更新列表内容的代码,因为我不知道将首先发送哪条消息(更新内容的消息或重新加载单元格的消息)。所以我认为最好的方法是,让您的自定义按钮子类对控制器有弱引用,这样按钮就可以更新内容,然后向控制器发送消息,并且基于相同的操作方法来工作控制器。
在你的按钮子类.h
在你的按钮子类.m
另外不要忘记正确配置单元格
Write an action method in the view controller which is the table view datasource, like :
Then in the
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
method, before returning the cell, configure it like so :This will make the
-(IBAction)reloadCell:(id)sender;
method called when touching the button, and reloading the particular cell, but this solution will maybe make you move the code that updates the content of the list, as I don't know which of the messages will be sent first (the one updates the content, or the one that reloads the cell).So the best way I think is, for your custom button subclass to have a weak reference to the controller, so the button could update the content, and then send a message to the controller, and is working based on the same action method in the controller.
In your button subclass .h
In your button subclass .m
Also don't forget to configure correctly the cell