我在UITableView中的索引位置0处动态添加单元格。所以每当我添加一个单元格,我希望它被选中,当我添加一个单元格,它将被选中时,我对一个选定的state.So有一个不同的图像。现在,在添加了几个单元格之后,用户可以在任何索引位置选择任何单元格。现在,当他再次开始添加单元格时,应该取消选择先前选择的单元格,并且应该选择索引位置为零的新添加的单元格。
为此,我编写了以下代码
- (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;
}
}发布于 2013-07-29 15:43:10
我认为这是因为您在两个if块中调用了didSelectRowAtIndexPath:和didDeselectRowAtIndexPath:。
尝试将这两行替换为:
[m_tableView deselectRowAtIndexPath:m_selectedCellIndexPath];和
[m_tableView selectRowAtIndexPath:m_selectedCellIndexPath];发布于 2014-08-14 19:33:31
希望这能对你有所帮助
- (void)setSelected:(BOOL)selected animated:(BOOL)animated;
// animate between regular and selected state假设您想要一些不同颜色的图像和文本,只需单击一个单元格即可
- (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
UITableViewCell *cellClicked;
if(cellClicked)
cellClicked.selected=NO;
cellClicked=[tableView cellForRowAtIndexPath:indexPath];
cellClicked.selected=YES;https://stackoverflow.com/questions/17917917
复制相似问题