0

我创建了一个带有标签和按钮的自定义 tableView。在一个按钮上单击一个单元格,我只想更改该单元格中的标签文本。我的问题是当我单击按钮时,其他单元格中的相应标签也会发生变化。这是我的代码

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier=@"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

if (cell == nil) { cell = [[[UITableViewCell alloc]
                            initWithStyle:UITableViewCellStyleDefault 
                            reuseIdentifier: CellIdentifier] autorelease];





        UILabel *labelOne = [[UILabel alloc]initWithFrame:CGRectMake(70, 10, 100, 20)];
        labelOne.tag = 100;
        labelOne.backgroundColor=[UIColor clearColor];
        labelOne.font=[UIFont systemFontOfSize:14];
        [cell.contentView addSubview:labelOne];
        [labelOne release];

        UIButton *minusbutton=[[UIButton alloc]initWithFrame:CGRectMake(152, 5, 15, 20)];

        minusbutton.backgroundColor=[UIColor clearColor];
        [minusbutton addTarget:self action:@selector(DecreaseTickets:) forControlEvents:UIControlEventTouchUpInside];
        [minusbutton setTitle:@"-" forState:UIControlStateNormal];
        [minusbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [minusbutton setBackgroundImage:[UIImage imageNamed:@"button-green1.png"] forState:UIControlStateNormal];
        [cell.contentView addSubview:minusbutton];
        [minusbutton release];


        UILabel *quantity = [[UILabel alloc]initWithFrame:CGRectMake(170, 5, 20, 20)];
        quantity.tag = 103;
        quantity.backgroundColor=[UIColor clearColor];
        quantity.font=[UIFont systemFontOfSize:12];
        [cell.contentView addSubview:quantity];
        [quantity release];

        UIButton *addbutton=[[UIButton alloc]initWithFrame:CGRectMake(192, 5, 15, 20)];

        addbutton.backgroundColor=[UIColor clearColor];
        [addbutton addTarget:self action:@selector(IncreaseTickets:) forControlEvents:UIControlEventTouchUpInside];
        [addbutton setTitle:@"+" forState:UIControlStateNormal];
        [addbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [addbutton setBackgroundImage:[UIImage imageNamed:@"button-green1.png"] forState:UIControlStateNormal];
        [cell.contentView addSubview:addbutton];
        [addbutton release];


        UILabel *labelTwo = [[UILabel alloc]initWithFrame:CGRectMake(260, 10, 140, 20)];
        labelTwo.tag = 101;
        labelTwo.backgroundColor=[UIColor clearColor];
        labelTwo.font=[UIFont systemFontOfSize:14];
        labelTwo.textColor=[UIColor colorWithRed:0.25098 green:0.447059 blue:0.07451 alpha:1];
        [cell.contentView addSubview:labelTwo];
        [labelTwo release];


        UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(70, 30, 230, 30)];
        label3.tag = 102;
        label3.backgroundColor=[UIColor clearColor];
        label3.numberOfLines=2;
        label3.font=[UIFont systemFontOfSize:12];
        [cell.contentView addSubview:label3];
        [label3 release];


}


    UILabel *labelOne = (UILabel *) [cell.contentView viewWithTag:100];
    NSString *ss2=[[NSString alloc]initWithFormat:@"%@",[[eventTicketListArray objectAtIndex:indexPath.row ]tit] ];

    labelOne.text = ss2;    

    UILabel *labelTwo = (UILabel *) [cell.contentView viewWithTag:101];
    labelTwo.text = [[NSString alloc]initWithFormat:@"%@",[[eventTicketListArray    objectAtIndex:indexPath.row ]price] ];

    UILabel *label3 = (UILabel *) [cell.contentView viewWithTag:102];
    label3.text=[[NSString alloc]initWithFormat:@"%@",[[eventTicketListArray objectAtIndex:indexPath.row ]desc ] ];

    UILabel *label4 = (UILabel *) [cell.contentView viewWithTag:103];
    label4.text=[[NSString alloc]initWithFormat:@"%d",qty];
}


return cell;

}

   Initially qty=0

现在我希望当我单击添加按钮时,该单元格的数量会增加。这是我的按钮点击事件

   -(void)IncreaseTickets:(id)sender{

NSIndexPath *indexPath =[ticketTable indexPathForCell:(UITableViewCell *)[[sender superview] superview]];

//NSUInteger row = indexPath.row;
  UITableViewCell *currentCell = (UITableViewCell *)[[sender superview] superview] ;

UILabel *label4 = (UILabel *) [currentCell.contentView viewWithTag:103];
label4.text=[[[NSString alloc]initWithFormat:@"%d",qty++] ;

//NSLog(@"%d",row);

[ticketTable reloadData];

}

但问题是当我单击按钮时,所有单元格的数量都会增加。请指导我如何做到这一点。任何帮助将不胜感激。

4

2 回答 2

0

你打电话时

 [ticketTable reloadData];

您的所有数据重新加载并且您的表再次初始化

UITable 的魔力在于重用一些已分配的单元格(只需最少数量的单元格:屏幕上一次可查看的单元格),然后您应该在滚动时使用新的数据设置重用它们,通常在表数据中使用 NSArray代表...

和: 数量在哪里定义?似乎它是一个全局变量,而不是每个单元格都有一个......

你最好阅读一些苹果示例代码或一些教程

于 2012-04-17T13:37:10.413 回答
0

像这样设置条件..在全局变量 NSUInteger myRow 上设置。

 myRow = indexPath.row;//Set Selected index path.

在 cellForRowAtIndexPath 设置条件。

 if(myRow == indexPath.row)
 {
     NSUInteger newqty = qty + 1;
     UILabel *label4 = (UILabel *) [cell.contentView viewWithTag:103];
     label4.text=[[NSString alloc]initWithFormat:@"%d",newqty];
 }
 esle
 {
     UILabel *label4 = (UILabel *) [cell.contentView viewWithTag:103];
     label4.text=[[NSString alloc]initWithFormat:@"%d",qty];
  }

并为此设定一些逻辑。

于 2012-04-17T13:39:45.470 回答