0

我有一个按钮和一个标签,它们是 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];

}

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

任何建议或代码

4

4 回答 4

0

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

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

于 2013-08-08T10:23:29.093 回答
0

请按照以下方式进行:

@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);
}

我希望这能帮到您。

于 2013-08-08T09:28:48.067 回答
0

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

于 2013-08-08T09:33:25.910 回答
0

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

于 2013-08-08T07:21:16.067 回答