简体   繁体   English

在Protocol方法之外更改UITableViewCell

[英]Change UITableViewCell outside of Protocol methods

The UITableViewCell is usually drawn with the UITableViewDelegate or UITableViewDataSource protocol methods, like 通常使用UITableViewDelegate或UITableViewDataSource协议方法绘制UITableViewCell,例如

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

But how would I change the cell outside of these methods? 但是如何在这些方法之外更改单元格? If I wanted to, for instance, turn a UITableViewCell's textLabel color to red using a button on the same screen, how would I refer to that cell and change it? 例如,如果我想使用同一屏幕上的按钮将UITableViewCell的textLabel颜色变为红色,我将如何引用该单元格并进行更改?

If I'm understanding your question correctly, assuming you know the row index of the cell you want to change you could simply have your button use the method: 如果我正确理解了您的问题,那么假设您知道要更改的单元格的行索引,则只需使用以下方法即可:

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

... to get a the UITableViewCell you want to edit and then use that pointer to change the cells textColor property etc. ...以获取要编辑的UITableViewCell,然后使用该指针更改单元格的textColor属性等。

UITableViewCell *firstCell = [someTableView cellForRowAtIndexPath:0];
firstCell.textColor = [UIColor redColor];

Easiest way is probably to have a variable that stores what color you want the row to be. 最简单的方法可能是使用一个变量来存储您希望行成为的颜色。

When you press the button, change the variable to the new color, then call [tableview reloadData] 当您按下按钮时,将变量更改为新颜色,然后调用[tableview reloadData]

Can you access the button from the tableView:cellForRowAtIndexPath: method? 您可以从tableView:cellForRowAtIndexPath:方法访问该按钮吗? If so, you can use an NSNotificationCenter to post notifications when the button is pressed and listen for those notifications from the table view cell (which you'll have to subclass). 如果是这样,您可以使用NSNotificationCenter在按下按钮时发布通知,并从表视图单元格(您必须对其进行子类化)中侦听这些通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM