【问题标题】:How to Handle Favourite button clicks in custom Tableview cells iOS?如何处理自定义 Tableview 单元格 iOS 中的收藏按钮点击?
【发布时间】:2015-01-28 08:04:33
【问题描述】:

我正在开发一个应用程序,我有一个要求。我必须处理自定义单元格上的收藏按钮,例如。我正在单元格上创建带有图像的自定义按钮,并设置未选择的类型我默认提供的收藏图像一旦用户单击单元格上的收藏按钮,我将更改按钮图像,如选定的收藏夹。我正在使用以下代码

- (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 等行。 任何人都可以建议我这样做的正确方法

提前致谢。

【问题讨论】:

    标签: ios iphone uitableview


    【解决方案1】:

    那个按钮动作和一切看起来都很好。您需要将选定的 Button 索引作为标签保存到 NSMutableArray 中,如下例所示:

    In.h 类:

    interface myclass : UIViewController{
    }
    @property (strong, nonatomic) NSMutableArray *arrcontainstate;
    

    In.m 类:

    - (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];
        }
    }
    

    【讨论】:

    • @nitin 工作正常......但是当我们滚动它时它会改变状态......那么如何解决它
    • 如果您注意到并检查我​​使用的 cellForRowindex 代码“if ([_arrcontainstate containsObject:indexPath.row]) {”,这意味着如果您的 idexPath 并且已经包含在 arraContainInstate 中,那么您应该设置状态我的意思是设置喜欢的图片
    【解决方案2】:

    当您重复使用单元格时,按钮的图像不会改变。第一个单元格在第 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;
    }
    

    【讨论】:

    • 我正在将图像设置为情节提要上的按钮。但是,一旦我单击第一行中的收藏按钮。第一行、第 6 行和第 11 行的按钮图像也发生变化
    • @user1915706 > 不要通过情节提要设置图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多