【发布时间】:2014-11-04 14:15:31
【问题描述】:
我创建了一个带有星形图像的按钮,我需要在单击时更改图像。比如点击按钮收藏,再次点击删除收藏。
这是代码
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:@"Cell"];
}
favButton = [UIButton buttonWithType:UIButtonTypeCustom];
[favButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[favButton setBackgroundImage:[UIImage imageNamed:@"stardes.png"] forState:UIControlStateNormal];
favButton.frame = CGRectMake(200, 90, 20, 25);
[cell.contentView addSubview:favButton];
return cell;
}
【问题讨论】:
-
将它的图像设置为 buttonClicked 中的新图像?
-
但是这种方法不起作用,因为当单元格被重绘时,它会再次用原始图像重绘。您需要保留“收藏”索引路径的列表,并在布局单元格时根据该索引路径是否“收藏”来选择和图像
标签: objective-c xcode ios7