我正在开发一个应用程序,我有一个要求。我必须处理自定义单元格上的收藏夹按钮。我正在创建自定义按钮,单元格上的图像和设置未选择的类型收藏夹图像我默认情况下,一旦用户点击单元格上的收藏夹按钮,我正在更改按钮图像,如所选的收藏夹。我使用以下代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @“CustomCell”;
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.favButton.tag = indexPath.section;
[cell.favButton addTarget:self action:@selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
按钮操作:
-(void)handleFavouriteButton:(id)sender
{
UIButton *button = sender;
NSLog(@"selected favourite button tag %li", (long)button.tag);
if (button.selected) {
[button setBackgroundImage:[UIImage imageNamed:@"favourites-normal.png"] forState:UIControlStateNormal];
}
else{
[button setBackgroundImage:[UIImage imageNamed:@"favourites-Selected.png"] forState:UIControlStateNormal];
}
button.selected=!button.selected;
}
使用上面的代码,我可以将收藏夹按钮的更改从正常更改为选中并选择为正常,但问题是当我选择第一行的收藏按钮时,它也会更改6和11 Ect ..行。 任何人都可以建议我这样做的正确方法
提前致谢。
答案 0 :(得分:1)
那个按钮动作和所有东西都很好看。您需要将选定的Button索引作为标记保存到NSMutableArray中,如下例所示:
In.ht课程:
interface myclass : UIViewController{
}
@property (strong, nonatomic) NSMutableArray *arrcontainstate;
In.m Class:
- (void)viewDidLoad {
[super viewDidLoad];
_arrcontainstate=[NSMutableArray array];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @“CustomCell”;
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.favButton.tag = indexPath.row;
if ( [_arrcontainstate containsObject:indexPath.row]) {
[cell.favButton setBackgroundImage:[UIImage imageNamed:@"favourites-Selected.png"] forState:UIControlStateNormal];
}
else {
[cell.favButton setBackgroundImage:[UIImage imageNamed:@"favourites-normal.png"] forState:UIControlStateNormal];
}
[cell.favButton addTarget:self action:@selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
-(void)handleFavouriteButton:(id)sender
{
UIButton *button = sender;
button.selected=!button.selected;
NSLog(@"selected favourite button tag %li", (long)button.tag);
if (button.selected) {
[_arrcontainstate addObject:button.tag];
[button setBackgroundImage:[UIImage imageNamed:@"favourites-Selected.png"] forState:UIControlStateNormal];
}
else
{
[_arrcontainstate removeObject:button.tag];
[button setBackgroundImage:[UIImage imageNamed:@"favourites-normal.png"] forState:UIControlStateNormal];
}
}
答案 1 :(得分:0)
当您重复使用单元格时,按钮的图像不会改变。第一个单元格在第6个和第11个单元格中重复使用,因此按钮图像保持选中状态。使用此:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @“CustomCell”;
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.favButton.tag = indexPath.section;
[cell.favButton setBackgroundImage:[UIImage imageNamed:@"favourites-normal.png"] forState:UIControlStateNormal];
[cell.favButton addTarget:self action:@selector(handleFavouriteButton:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}