我的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