选中取消选中uitableview单元格中的按钮 [英] Check Uncheck buttons in uitableview's cell

查看:116
本文介绍了选中取消选中uitableview单元格中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前看过一些帖子,但还没有得到答案,这就是为什么我想以更有效的方式再次发帖。如何在 UITableView 中使用check-uncheck功能,如下图所示。

I have seen some posts before, but didn't get the answer yet, thats why i am trying to post again in more effective manner. How can i use check-uncheck functionality in UITableView like below image.

这是我想要点击的表格在任何单元格的按钮上,按钮图像将改变,而不是在所有单元格上。

This is table i want when i click on button of any cell, that buttons image will change, not on all cells.

推荐答案

仅针对Check-Uncheck功能 buttonClicked:方法还不够。您还将条件放在 cellForRowAtIndexPath:方法中,其中选中按钮或取消选中,因为 cellForRowAtIndexPath:方法将在每次滚动 UITableView 时调用,并且将刷新单元格。
我看到你之前的问题,你添加两个按钮,两个动作不是一个好方法只需更改按钮的图像 check-uncheck

For Check-Uncheck functionality only buttonClicked: method is not enough. You will have also put the condition in cellForRowAtIndexPath: method for which button is selected or which in unselected because cellForRowAtIndexPath: method will call each time when you will scroll your UITableView and cells will be refresh. And i saw your previous question you're adding two buttons with two action not a good way just change the image of button for check-uncheck.

所以这就是我为此做的事情 -

So here is what i do for this -

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
{
    IBOutlet UITableView *tblView;
    NSMutableArray *arrayCheckUnchek; // Will handle which button is selected or which is unselected
    NSMutableArray *cellDataArray; // this is your data array
}

@end

现在在 ViewController.m class -

Now in ViewController.m class -

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    arrayCheckUnchek = [[NSMutableArray alloc]init];
    //Assign your cell data array 
    cellDataArray = [[NSMutableArray alloc]initWithObjects:@"cell-1",@"cell-2",@"cell-3",@"cell-4",@"cell-5", nil];

    // setting all unchecks initially
    for(int i=0; i<[cellDataArray count]; i++)
    {
        [arrayCheckUnchek addObject:@"Uncheck"];
    }

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [cellDataArray count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [cellDataArray objectAtIndex:indexPath.row];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)];

    if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
    [button setImage:[UIImage imageNamed:@"uncheck_icon"] forState:UIControlStateNormal];
    else
    [button setImage:[UIImage imageNamed:@"check_icon"] forState:UIControlStateNormal];

    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:button];

    return cell;
}

-(void)buttonClicked:(id)sender
{
    //Getting the indexPath of cell of clicked button

    CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tblView];
    NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:touchPoint];

    // No need to use tag sender will keep the reference of clicked button
    UIButton *button = (UIButton *)sender;

    //Checking the condition button is checked or unchecked.
    //accordingly replace the array object and change the button image
    if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
    {
        [button setImage:[UIImage imageNamed:@"check_icon"] forState:UIControlStateNormal];
        [arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Check"];
    }
    else
    {
        [button setImage:[UIImage imageNamed:@"uncheck_icon"] forState:UIControlStateNormal];
        [arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];
    }
}

最后它看起来像 -

And finally it will look like -

这篇关于选中取消选中uitableview单元格中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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