选中取消选中uitableview单元格中的按钮

时间:2012-10-10 07:23:04

标签: iphone uitableview

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

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

table

5 个答案:

答案 0 :(得分:7)

对于Check-Uncheck功能,仅buttonClicked:方法是不够的。您还会在cellForRowAtIndexPath:方法中选择选择按钮,或者取消选择,因为cellForRowAtIndexPath:方法每次都会调用将滚动您的UITableView,细胞将刷新。 我看到你之前提出的问题,即你只需更改check-uncheck按钮的图像,就可以通过两个动作添加两个按钮。

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

#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班级 -

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

最后它看起来像 -

enter image description here

答案 1 :(得分:0)

标签对于IB中设计的自定义uitableviewcell非常有用。如果以编程方式创建单元格,则不需要标记。您只能使用它们来找到正确的uiview来设置属性。

答案 2 :(得分:0)

是的,在UITableViewCell中声明类并使用标签识别它们时,设置标签很简单。我已经发布了一些示例代码供您参考。我不确定它会解决你的问题。您询问了如何使用标记设置标记和标识类。所以它会给你一些关于你的问题的基本想法。

-(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] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        label1 = [[UIView alloc] initWithFrame:CGRectMake(2, 20, 15, 15)];
        label1.tag = 100;
        label1.backgroundColor = [UIColor whiteColor];
        label1.layer.cornerRadius = 5;
        [cell.contentView addSubview: label1];

        label2 = [[UILabel alloc] initWithFrame:CGRectMake(25, 2, 165, 23)];
        label2.tag = 101;
        label2.font = [UIFont boldSystemFontOfSize:15];
        label2.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview: label2];

        label3 = [[UILabel alloc] initWithFrame:CGRectMake(190, 2, 90, 23)];
        label3.tag = 102;
        label3.font = [UIFont systemFontOfSize:14];
        label3.textColor = [UIColor colorWithRed:0.14117670588235 green:0.435294117647059 blue:0.847058823529412 alpha:1];
        label3.backgroundColor = [UIColor clearColor];
        label3.textAlignment = UITextAlignmentRight;
        [cell.contentView addSubview: label3]; 
    }

   label1 = (UILabel *) [cell.contentView viewWithTag:100];
    NSString *string1 = [array1 objectAtIndex:indexPath.row];

  label2 = (UILabel *) [cell.contentView viewWithTag:101];
    NSString * string2 = [array2 objectAtIndex:indexPath.row];

  label3 = (UILabel *) [cell.contentView viewWithTag:102];
    NSString * string3 = [array3 objectAtIndex:indexPath.row];

   return cell;   
} 

请告诉我这是否有用。否则我们会采取其他方式。

快乐编码。感谢。

答案 3 :(得分:0)

我同意@VakulSaini,因为你可以做到这一点来处理哪个细胞触摸或滑动或者什么,这是一个前任:

-(void)handleSwipLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
    CGPoint Location = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:Location];
    cell = [tableView cellForRowAtIndexPath:indexPath];
}

答案 4 :(得分:0)

使用cellForRowAtIndexPath的{​​{1}}方法,并在此方法中添加任何组件,如(UITableViewUITextField .....等)在UILabel使用UITableViewCell并按[cell.contentView addSubview:YourComponentName];为每个组件提供标记

如,,,

indexPath.row