在按下状态并选择状态时更改UITableViewCell的颜色 [英] Changing color of UITableViewCell on state pressed and state selected

查看:127
本文介绍了在按下状态并选择状态时更改UITableViewCell的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有customCell(具有2个标签和一个ImageView的CustomCell)的UITableView。

I am having UITableView with customCell(CustomCell having 2 lables and One ImageView).

在正常模式下,我需要所有单元格为白色。

In normal Mode I need white color of all the cells.

如果用户按下特定的单元格,则该单元格的颜色应为灰色(其余单元格应为白色)

If user press the particular cell, then color of that cell should be gray(rest of the cells should be white)

并且,如果用户释放相同的单元格,则该单元格的颜色应为橙色(其余单元格应为白色)

And If user release the same cell, Color of that cell should be Orange(rest of the cells should be white)

我怎么知道呢?

我尝试使用setSelectedBackground,willSelectRowAtIndexPath和Gestures方法进行此操作。但是无法看到同一单元格的这3种颜色状态。

I have tried this using setSelectedBackground, willSelectRowAtIndexPath and Gestures methods. But can't see these 3 Color states for the same Cell. Any Of the 2 states are working together.

任何想法我如何实现相同的功能?

Any Idea how can i achieve the same functionality?

我已使用选择器在android中实现了相同的功能。我想要iPhone中的相同功能。有帮助吗?

I have implemented same functionality in android using selectors. I want the same functionality in iPhone. Any Help?

谢谢!

推荐答案

如果需要灰色然后

cell.selectionStyle=UITableViewCellSelectionStyleGray;  

或者您可以在 didSelectRow 上设置背景颜色

Or you can set background color on didSelectRow

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView reloadData];
    UITableViewCell *cell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor orangeColor]];
}  

如果您不想重新加载tableData,则必须保存以前的然后索引值

If you don't want to reload tableData then you have to save your previousSelected Index value then

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Make oreange cell
    UITableViewCell *presentCell=(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
    [presentCell setBackgroundColor:[UIColor orangeColor]];

    //make your previous cellBackgrod to clear, you can make it white as per your requirement
    UITableViewCell *previouscell=(UITableViewCell*)[tableView cellForRowAtIndexPath:previousSelectedCellIndexPath];
    [previouscell setBackgroundColor:[UIColor clearColor]];

    //save your selected cell index
    previousSelectedCellIndexPath=indexPath;
}

这篇关于在按下状态并选择状态时更改UITableViewCell的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆