Iphone 单击自定义单元格中的按钮时如何更改该单元格中的标签文本

Iphone 单击自定义单元格中的按钮时如何更改该单元格中的标签文本,iphone,uitableview,Iphone,Uitableview,我已经创建了一个带有标签和按钮的自定义tableView。在单元格中单击按钮时,我只想更改该单元格中的标签文本。我的问题是当我点击按钮时,其他单元格中相应的标签也会发生变化。这是我的密码 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier=@"CellIdentifier"; U

我已经创建了一个带有标签和按钮的自定义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;
}

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

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

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

当你打电话时

 [ticketTable reloadData];
重新加载所有数据并再次初始化表

UITable magic是重用几个分配的单元格,即所需的最小单元格数:屏幕上一段时间内的可视单元格,然后在滚动时,您应该使用新的数据设置重用它们,通常在表数据委托中使用NSArray

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

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

这样设置条件。。 在全局变量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];
  }

并为此设置一些逻辑。

您最好编写一个自定义单元格,其中包含按钮和标签。然后,该单元处理按钮按下,并使用代理向控制器发回信号。
 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];
  }