我有一个TableView中有多行。每行都有不同的自定义UITableViewCell。我需要在UIActionSheet的帮助下改变这个单元格的颜色。这意味着当我选择一行时,应该弹出一个Actionsheet,要求为该单元格选择特定的颜色。另一个重要的是细胞应该保留颜色,即使细胞离屏。如何使用UIActionSheet更改自定义UITableViewCell的颜色?
这是我的代码。我的代码的问题是单元格没有实时更新。如果我再次选择该行,单元格的颜色会更新。另一个问题是,如果我向下滚动单元格颜色更改为默认白色。
UIColor *cellColour;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
switch (indexPath.row)
{
case 0:
[self displayActionSheet];
cell.backgroundColor=cellColour;
break;
case 1:
cell.backgroundColor=[UIColor yellowColor];
break;
default:
break;
}
}
-(void) displayActionSheet
{
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:@"Select row colour" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Red",@"Green",nil];
popupQuery.actionSheetStyle = UIActionSheetStyleDefault;
[popupQuery showInView:self.view];
[popupQuery release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case 0:
NSLog(@"Red");
cellColour=UIColor.redColor;
break;
case 1:
NSLog(@"Green");
cellColour=UIColor.greenColor;
break;
case 2:
NSLog(@"Pressed Cancel");
cellColour=nil;
break;
default:
break;
}
}
请帮忙。
请修改您的帖子以更正代码格式。 – Rengers