在特定单元格的标签中显示按钮单击计数,这些单元格是uitableview的子视图

最后发布: 2013-08-08 07:11:19


问题

我有一个按钮和一个标签,它们是UITableView的子视图。

最初的标签值为0。

我需要的是,当我单击特定单元格上的按钮时,我想增加同一单元格标签中的值(为1)并在同一标签中显示该值。

再次单击同一单元格按钮,该单元格中的标签应为增量(为2)并在UITableView的同一单元格中显示该值。

我的代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *Lbl;
UIButton *btn;
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    Lbl =[[UILabel alloc]init];
    [Lbl setFrame:CGRectMake(56, 60, 117, 12)];
    [Lbl setBackgroundColor:[UIColor clearColor]];
    [Lbl setTextAlignment:NSTextAlignmentLeft];
    [Lbl setFont:[UIFont boldSystemFontOfSize:15.0]];
    Lbl.tag=indexPath.row;
    [cell.contentView addSubview:Lbl];

    btn =[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"add" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateHighlighted];
    [[btn titleLabel] setFont:[UIFont boldSystemFontOfSize:23]];
    [btn setFrame:CGRectMake(289, 2, 30, 71)];
    btn.tintColor=[UIColor lightGrayColor];
    btn.tag=indexPath.row;
    [btn addTarget:self action:@selector(increaseItemCount:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:btn];
} else {
    Lbl =(UILabel *)[cell.contentView viewWithTag:indexPath.row];
    btn =(UIButton *)[cell.contentView viewWithTag:indexPath.row];
}

cell.textLabel.text=@"title";
countLbl.text = [[NSString alloc] initWithFormat:@"%d",showItemCount];

return cell;

}

// button action method
-(void)increaseItemCount:(UIButton *)sender
{    
UITableViewCell *cell = (UITableViewCell *)sender.superview.superview;
NSIndexPath *path = [tableView indexPathForCell:cell];
NSLog(@"row: %d",path.row);
UILabel *countLbl =(UILabel *)[cell.contentView viewWithTag:path.row];
showItemCount=[countLbl.text intValue] + 1;
NSLog(@"%d",showItemCount);
countLbl.text = [[NSString alloc] initWithFormat:@"%d",showItemCount];

}

我试过了,单击该值后,其他单元格中将显示该值,当我滚动表格视图时,该值将在所有单元格中显示。

任何建议或代码

iphone ios uitableview uibutton uilabel
回答

那是因为看起来您正在使用单个实例变量来存储itemCount。 您应该使用数组来知道哪个单元格被单击了多少次。 但是代码很乱,因此您应该从头开始编写


回答

请按照以下步骤进行:

@implementation ViewController

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   // UILabel *Lbl;
   // UIButton *btn;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        UILabel *Lbl =[[UILabel alloc]init];
        [Lbl setFrame:CGRectMake(56, 60, 117, 12)];
        [Lbl setBackgroundColor:[UIColor clearColor]];
        [Lbl setTextAlignment:NSTextAlignmentLeft];
        [Lbl setFont:[UIFont boldSystemFontOfSize:15.0]];
        Lbl.tag=indexPath.row;
        Lbl.text=[NSString stringWithFormat:@"%i",0];
        [cell.contentView addSubview:Lbl];

        UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"add" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateHighlighted];
        [[btn titleLabel] setFont:[UIFont boldSystemFontOfSize:23]];
        [btn setFrame:CGRectMake(289, 2, 30, 71)];
        btn.tintColor=[UIColor lightGrayColor];
        btn.tag=indexPath.row;
        [btn addTarget:self action:@selector(increaseItemCount:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btn];

 NSLog(@"Selected Index:%i",indexPath.row);   
    } else {

       UILabel *Lbl =(UILabel *)[cell.contentView viewWithTag:indexPath.row];
      //UIButton *btn =(UIButton *)[cell viewWithTag:indexPath.row];

        if(indexPath.row==selectedIndex)
        {
             NSLog(@"Selected Index in CellForRowAtIndexPath:%i",indexPath.row);
            Lbl.text=[NSString stringWithFormat:@"%i",[Lbl.text intValue]+1];
        }

    }

   // cell.textLabel.text=@"title";
    //countLbl.text = [[NSString alloc] initWithFormat:@"%d",showItemCount];

    return cell;
}

-(void)increaseItemCount:(UIButton *)sender
{
    selectedIndex=sender.tag;
    [tblView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Selected Index:%i",indexPath.row);
}

我希望这能帮到您。


回答

indexPath.row0到n开始,我们没有为标签设置标签0 ,而是使用单个变量showItemCount用于在countLbl分配文本, countLbl表视图重用单元格出现时,使用数组为每个单元格存储showItemCount


回答

如果您想要源代码,请从这里https://github.com/MasudShuvo/TestCustomTableViewCell下载

我已根据您的要求修改了代码。