我想在选择时在单元格上显示图像。我已经尝试过无穷无尽的方法。
我已尝试将其添加到didSelectRowAtIndexPath
:
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.imageView.image = [UIImage imageNamed:@"deselected_image.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:@"selected_image.png"];
但是图像在推动时不会改变。
由于
答案 0 :(得分:0)
您拥有的代码是创建新的表格视图单元格 - 您需要检索所选的当前单元格。请尝试以下方法:
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"deselected_image.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:@"selected_image.png"];
}
答案 1 :(得分:0)
我已根据要求将UIColor应用于每个单元格。您可以替换UIColor代码&应用图像。 这是我的代码
- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath
{
if(tableView==tblLanguage)
{
cell.backgroundView = [[UIView alloc] init];
((UIView *)cell.backgroundView).backgroundColor = !(indexPath.row % 2)?[UIColor colorWithRed:(float)231/255 green:(float)231/255 blue:(float)231/255 alpha:1.0]:[UIColor colorWithRed:(float)218/255 green:(float)218/255 blue:(float)218/255 alpha:1.0];
((UIView *)cell.backgroundView).alpha=1.0;
//ilangSelectedIndex is set in the didselect row of the tableview delegate function
if(ilangSelectedIndex==[indexPath row])
{
((UIView *)cell.backgroundView).backgroundColor=[UIColor colorWithRed:(float)171/255 green:(float)177/255 blue:(float)213/255 alpha:1.0];
}
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
}
在表格的选择功能中,您将表格的ilangSelectedIndex设置为Indexpath.row 。 重新加载表格以查看效果。
答案 2 :(得分:0)
在didSelectRowAtIndexPath中尝试此代码:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.selectedBackgroundView=[[UIImageView alloc]init];
cell.backgroundView =[[UIImageView alloc] init];
((UIImageView *)cell.selectedBackgroundView).image=[UIImage imageNamed:@"selected.png"];