2011-11-08 96 views
0

我在我的应用程序中使用UITableView,要求是当我按下我的UITableView的一个单元转到下一视图时,应该更改该特定单元格的颜色变成蓝色。如何在选择时将UITableViewCell的颜色更改为蓝色

我该如何做到以下是我的代码。

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

    static NSString *MyIdentifier = @"dealsCC"; 
    dealsCC *cell = (dealsCC *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    [cell selectionStyle:UITableViewCellSelectionStyleBlue]; 
} 

请告诉我,

thanx提前。

回答

0

设置selectionStyle。

[cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; 
+1

我已经尝试了这一点,但它无法正常工作。 – iPhone

0
在didSelectRowAtIndexPath方法

,你必须编写代码

UITableViewCell *cell = [table cellforRowAtIndexPath:indexPath.row]; 
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue]; 
+0

我已经尝试过,但它不工作有没有其他方法? – iPhone

+0

在cellforRowAtIndexPath [cell setSelectionStyle:UITableViewCellSelectionStyleBlue]中写入以下代码; – Tendulkar

+0

其实我写了下面的代码: – iPhone

0
// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"myCellId"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     UIView *v = [[[UIView alloc] init] autorelease]; 
     v.backgroundColor = [UIColor redColor]; 
     **cell.selectedBackgroundView = v;** 
    } 
    // Set up the cell... 
    cell.textLabel.text = @"foo"; 
    return cell; 
} 
0

叶氏。 我不喜欢这样

UIView *bgColorSelected = [[UIView alloc] init]; 
    [bgColorSelected setBackgroundColor:[UIColor colorWithRed:180.0f/255.0f green:36.0f/255.0f blue:21.0f/255.0f alpha:1.0f]]; 
    cell.selectedBackgroundView = bgColorSelected; 
    [bgColorSelected release]; 
    cell.backgroundColor = [ UIColor clearColor]; 
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = 
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier]; 
    } 

    cell.selectionStyle = UITableViewCellSelectionStyleBlue; 

    return cell; 
} 
0
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ 
    cell.selectionStyle = UITableViewCellSelectionStyleBlue; 
} 
相关问题