3

我以前看过一些帖子,但还没有得到答案,这就是为什么我试图以更有效的方式再次发布。我如何使用UITableView下图中的检查取消检查功能。

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

桌子

4

5 回答 5

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

最后它看起来像 -

在此处输入图像描述

于 2012-10-10T08:19:27.920 回答
0

我同意@VakulSaini,因为你可以这样做来处理哪个单元格触摸或滑动或任何事情,这是一个前任:

-(void)handleSwipLeft:(UISwipeGestureRecognizer *)gestureRecognizer
{
    CGPoint Location = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:Location];
    cell = [tableView cellForRowAtIndexPath:indexPath];
}
于 2012-10-10T07:32:36.153 回答
0

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

于 2012-10-10T07:30:32.947 回答
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;   
} 

请让我知道这是否有用。否则,我们将采用其他方式。

快乐编码。谢谢。

于 2012-10-10T07:32:07.140 回答
0

使用cellForRowAtIndexPath方法和 in 这个方法在你的 by using UITableView中添加任何组件,如 ( UITextField, UILabel..... etc) ,并将每个组件赋予它的标签by 。UITableViewCell[cell.contentView addSubview:YourComponentName];indexPath.row

比如,,,

YourComponentName.tag = indexPath.row;
于 2012-10-10T07:43:07.833 回答