如何处理自定义Tableview单元格iOS中的收藏夹按钮单击? [英] How to Handle Favourite button clicks in custom Tableview cells iOS?

查看:52
本文介绍了如何处理自定义Tableview单元格iOS中的收藏夹按钮单击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,但我有一个要求.我必须处理类似自定义单元格上的收藏夹"按钮.我正在使用单元格上的图像创建自定义按钮,并设置未选择的类型.我会在用户单击单元格上的收藏夹"按钮后默认提供我喜欢的图像,我会像选择收藏夹一样更改按钮图像.我正在使用以下代码

I am developing one app and i have one requirement that. I have to handle the Favourite button on custom cells like. i am creating the custom button with image on cell and setting unselected type Favourite image i am giving by default once user click on the Favourite button on cell i am changing the button image like selected favourite. I am using the following code

- (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 ..行.有人可以建议我这样做的正确方法

Using the above code i am able to change the change the Favourite button from normal to selected and selected to normal but problem is when i select the favourite button on first row it is changing 6 and 11 Ect.. rows also. Can anybody suggest me right way to do this

谢谢.

推荐答案

该按钮动作和所有内容都很好.您需要将选定的Button索引作为标签保存到NSMutableArray中,如下例所示:

That button action and all things are looks fine. You need to save selected Button index as a tag in to the NSMutableArray like following example:

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

这篇关于如何处理自定义Tableview单元格iOS中的收藏夹按钮单击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆