首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在UITableView中的索引路径处DeSelect行

无法在UITableView中的索引路径处DeSelect行
EN

Stack Overflow用户
提问于 2013-07-29 15:07:37
回答 2查看 929关注 0票数 1

我在UITableView中的索引位置0处动态添加单元格。所以每当我添加一个单元格,我希望它被选中,当我添加一个单元格,它将被选中时,我对一个选定的state.So有一个不同的图像。现在,在添加了几个单元格之后,用户可以在任何索引位置选择任何单元格。现在,当他再次开始添加单元格时,应该取消选择先前选择的单元格,并且应该选择索引位置为零的新添加的单元格。

为此,我编写了以下代码

代码语言:javascript
复制
- (void)selectRow0
{

    NSLog(@"row:%d",array.count);

    if(row > 1)
    {
        NSLog(@"indexpathoCheck%@",m_selectedCellIndexPath);
        [self tableView:m_tableView didDeselectRowAtIndexPath:m_selectedCellIndexPath];

    }

    if(row > 0)

        m_selectedCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [m_tableView selectRowAtIndexPath:m_selectedCellIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
        [self tableView:m_tableView didSelectRowAtIndexPath:m_selectedCellIndexPath];

}


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

    TestCell *cell = (TestCell*)[tableView cellForRowAtIndexPath:indexPath];  

    cell.customImageView.image = selectedImage;

}

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


        TestCell *cell = (TestCell*)[tableView cellForRowAtIndexPath:indexPath];
        [cell.customImageView.image = nil;
    }
}
EN

回答 2

Stack Overflow用户

发布于 2013-07-29 15:43:10

我认为这是因为您在两个if块中调用了didSelectRowAtIndexPath:didDeselectRowAtIndexPath:

尝试将这两行替换为:

代码语言:javascript
复制
[m_tableView deselectRowAtIndexPath:m_selectedCellIndexPath];

代码语言:javascript
复制
[m_tableView selectRowAtIndexPath:m_selectedCellIndexPath];
票数 1
EN

Stack Overflow用户

发布于 2014-08-14 19:33:31

希望这能对你有所帮助

代码语言:javascript
复制
- (void)setSelected:(BOOL)selected animated:(BOOL)animated; 
        // animate between regular and selected state

假设您想要一些不同颜色的图像和文本,只需单击一个单元格即可

代码语言:javascript
复制
 - (void)setSelected:(BOOL)selected animated:(BOOL)animated
{

      [super setSelected:selected animated:animated];
    if(selected)
        {

                self.image  =[UIImage imageNamed:@"some_image.png"];
                self.lbl.textColor=[UIColor whiteColor];

        }
    else
   {

                self.image  =[UIImage imageNamed:@"some_ another _image.png"];
                self.lbl.textColor=[UIColor grayColor];
   }

}

并将此代码添加到您的didSelectRowAtIndexPath

代码语言:javascript
复制
 UITableViewCell    *cellClicked;

 if(cellClicked)
    cellClicked.selected=NO;
    cellClicked=[tableView cellForRowAtIndexPath:indexPath];
    cellClicked.selected=YES;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17917917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档