[英]Change a label in a prototype cell upon button press
I have set up a tableview using custom cells as such:- 我已经使用自定义单元格设置了一个tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{SWHNearYouCell *cell = (SWHNearYouCell *)[tableView
dequeueReusableCellWithIdentifier:@"NearYouCell"];
SWHNearYou *aPointer = [self.thingsNearYou objectAtIndex:indexPath.row];
cell.customNearYouLabel.text = aPointer.restrauntsNearYou;
return cell;
}
I want to change the text of customNearYouLabel upon a button press but work out how to get a pointer to the cell in my -IBAction method. 我想在按下按钮时更改customNearYouLabel的文本,但要弄清楚如何在我的-IBAction方法中获取指向单元格的指针。
Thanks 谢谢
You can just handle that in your tableView:didSelectRowAtIndexPath:
method by grabbing the cell. 您可以通过抓取单元格在
tableView:didSelectRowAtIndexPath:
方法中进行处理。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.customNearYouLabel.text = @"New Text";
}
I presume the button is not on a table cell? 我假设按钮不在表格单元格上?
If so, you just need to update the relevant value in the self.thingsNearYou
array. 如果是这样,您只需要更新
self.thingsNearYou
数组中的相关值self.thingsNearYou
。
If you then call [tableView reloadData] then the table will reload it's data and the text of customNearYouLabel
will change. 如果然后调用[tableView reloadData],则表将重新加载其数据,并且
customNearYouLabel
的文本将更改。
Worked it out - needed to add in self before tableview 解决了-需要在tableview之前添加self
-(IBAction)cancel:(id)sender{
SWHNearYouCell *cell = (SWHNearYouCell *)[self.tableView
dequeueReusableCellWithIdentifier:@"NearYouCell"];
cell.customNearYouLabel.text = @"New Text";
NSLog(@"This is it: %@",cell.customNearYouLabel.text);
}
I'll need to spend some more time on it as [self.tableView reloadData]; 我需要花更多时间在[self.tableView reloadData]上; will not update the table but I reckon that should be easier to solve.
不会更新表格,但我认为应该更容易解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.