2013-10-26 65 views
0

我已经子类化了一个UITableViewCell,这样我就可以增加滚动性能,这对我来说已经非常有用。Subclassed UItableViewCell选择

在我的子类,我有一个名为seSelected方法,看起来像这样

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
    if (selected) { 
     self.backgroundColor = [UIColor lightGrayColor]; 
    }else{ 
     self.backgroundColor = [UIColor whiteColor]; 
    } 
} 

我想知道如何让这个如果我触摸同一小区将取消选择单元格,并改变颜色重新白色?我已经尝试了一些不同的如果在setSelected但nothings工作的说法。

任何帮助,将不胜感激。

回答

0

使用该委托的方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

,并呼吁在这里它们管理选择的方法和未选中单元格。

0

一种方法可以是在单元上设置标签,然后用它来设置背景颜色。

typedef enum { 
    CellSelected = 0, 
    CellDeselected 
} CellSelection; 

在创建单元设置标记为“CellDeselected”

cell.tag = CellDeselected; 

然后当细胞被窃听只是检查要在背景设置的颜色。

switch (customCell.tag) { 
    case CellSelected: 
     self.backgroundColor = [UIColor lightGrayColor]; 
     break; 
    case CellDeselected: 
     self.backgroundColor = [UIColor whiteColor]; 
     break; 
    default: 
     break; 
} 

customCell.tag = !customCell.tag; 
0

做这样的...

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    str = [YourArray objectAtIndex:indexPath.row];//str is a string variable 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    NSString *cellIndentifier = @"cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIndentifier]; 
    } 
    cell.textLabel.text = [reminder objectAtIndex:indexPath.row]; 

    cell.backgroundColor = [UIColor whiteColor]; 

    if ([str isEqualToString:[reminder objectAtIndex:indexPath.row]]) 
    { 
     cell.backgroundColor = [UIColor grayColor]; 
    } 

    return cell; 
} 

您可以使用此为您的自定义单元格.....

0

使用的UITableViewCell方法:[cell setSelectedBackgroundView:myBgColorView];Apple Documentation。 例子:

UIView *myBgColorView = [[UIView alloc] init]; 
myBgColorView.backgroundColor = [UIColor greenColor]; 
[cell setSelectedBackgroundView:myBgColorView]; 
0

设置单元格样式:

cell.selectionStyle = UITableViewCellSelectionStyleNone; 

,你的代码将工作。但是,您只设置颜色选定的单元格。 如果您在按下单元格时需要设置颜色,请覆盖此方法:

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated