我的UIButton
在我的UITableViewCell
。此按钮启动UIActionSheet
,因此当用户从操作表中按下按钮时,我希望按钮的文本更改为该文本。从UIActionSheet更新UIButton文本
我用下面的代码显示按钮。我想如果我在actionSheet: clickedButtonAtIndex:
中调用'tableView reload'会有效,但是有更好的选择来更新statusButton
吗?
SmokingStatusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.statusButton.titleLabel.text = [self.vitalsDictionary objectForKey:@"smoking_status_display"];
- (IBAction)smokingStatusButtonClicked:(id)sender{
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"Smoking Status" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"1 Current everday smoker", @"2 Current some day smoker", nil];
self.smokingStatusActionSheet = actionSheet;
[self.smokingStatusActionSheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (actionSheet == self.smokingStatusActionSheet){
switch (buttonIndex) {
case 0:
[self.vitalsDictionary setObject:@"daily_smoker" forKey:@"smoking_status"];
break;
case 1:
[self.vitalsDictionary setObject:@"nondaily_smoker" forKey:@"smoking_status"];
break;
default:
break;
}
}
}
我拥有的代码非常复杂,我宁愿不将这些方法分开。有没有办法更新包含按钮的特定单元格?它的'indexPath'是9. – Jon
就我所能想象的那样,在分离代码时不应该有任何困难,因为在你取回一个单元格之后,在你返回并粘贴到新方法中之前,它将被删除。但无论如何,indexPath 9没有这样的东西。indexPath可以有很多索引,但是对于tableview它们通常只有两个'section'和'row'。我会从你的“9”中假设你只有一个部分,所以为了得到那个单元格,你需要'[NSIndexPath indexPathForRow:9 inSection:0]对这些硬编码的魔术数字保持警惕,他们很可能会咬人你以后。 –
谢谢。我尝试过,但它似乎没有做任何事情:'NSArray * array = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:9 inSection:0]]; [self。vitalsTableView reloadRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationNone];' – Jon