自定义 UITableViewCell 无法选中 点击颜色不变

本文转自 http://www.999dh.net/article/iphone_ios_art/35.html  转载请注明,谢谢!

http://www.cnblogs.com/rollrock/archive/2012/12/04/2801524.html

在上面的这篇文章里面,实现了自定义 UITableViewCell实现多样化的效果

今天看了QQ上的cell,发现最左边头像的右上角会有一个未读消息的红色数字icon,觉得应该把这个功能实现
然后在使用自己定义的cell的时候,发现点击的时候,cell的背景颜色没有变化,(长按颜色是可以改变的)
然后在网上找了很多,最后发现问题在这里
如果 willSelectRowAtIndexPath 返回nil的话,那函数didSelectRowAtIndexPath就没办法促发,也就出现了我上面的问题。

An index-path object that confirms or alters the selected row. Return an NSIndexPath object other than indexPath if you want another cell to be selected. Return nil if you don'€™t want the row selected.

/*
-(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"aaa:%d",indexPath.row);
    
    return nil;
}
*/

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"selected:%d",indexPath.row);
}

这样这个问题就解决了,嘿嘿,看来很多东西还是要做了才知道问题的所在的。

原文地址:https://www.cnblogs.com/rollrock/p/2809250.html