2012-09-15 57 views
1

我有一个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; 
    } 
} 

请帮忙。

+0

请修改您的帖子以更正代码格式。 – Rengers

回答

2

这很正常,因为UIActionSheet行为是异步的。

当您拨打displayActionSheet时,它会在屏幕上显示UIActionSheet,然后继续执行代码(无需等待用户点击操作表按钮)。然后稍后当用户点击其中一个操作表的按钮时,调用委托方法actionSheet: clickedButtonAtIndex:

你需要做的是:

  • 在使用cellColor属性(我希望能在现实中,它是你的类的@property和像你的问题的代码不是一个全局变量!)当用户在动作片已选择的颜色,因此tableView:cellForRowAtIndexPath:方法(设置cell.backgroundColor = cellColour;这里),使得颜色用于每个单元的再生时,在你actionSheet:clickedButtonAtIndex:委托方法来重新加载的tableView屏幕上显示
  • 呼叫[tableView reloadData]细胞颜色被更新。
+0

我尝试过,但仍然无法正常工作。 – Sibin

+0

您是否尝试在代码中记录所有内容并逐步执行? – AliSoftware

+0

我尝试了一切。没有运气。你能否提供一些示例代码? – Sibin

相关问题