0

我创建了一个自定义单元格

在此处输入图像描述

在这个“+”号中增加票数,“-”号减少票数。当票数为 0 时,禁用“-”,当票数为 4 时,禁用“+”。到目前为止它工作正常。问题是现在我想在单击第 4 行的“+”按钮时更改表格的框架大小,并在单击第 4 行的“-”时重新获得框架大小。当我这样做时,其他行的“-”功能会随机变化。请让我知道我做错了什么。谢谢这是我的代码

    -(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 = [[UI

Label 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];

            minusbutton=[[UIButton alloc]initWithFrame:CGRectMake(152, 5, 15, 20)];
            minusbutton.tag=104;
            [minusbutton setEnabled:NO];
            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];

            addbutton=[[UIButton alloc]initWithFrame:CGRectMake(192, 5, 15, 20)];
            addbutton.tag=105;
            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(240, 10, 80, 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];
        NSString *string2=[[NSString alloc]initWithFormat:@"%@",[[eventTicketListArray  objectAtIndex:indexPath.row ]price] ];

            labelTwo.text =string2;




        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",[[eventTicketListArray objectAtIndex:indexPath.row ]qty ] ];


    }



    return cell;


     }

+ 按钮单击的方法

 -(void)IncreaseTickets:(id)sender{
    NSIndexPath *indexPath =[ticketTable indexPathForCell:(UITableViewCell *)[[sender superview] superview]];
    NSUInteger row = indexPath.row;




    UITableViewCell *currentCell = (UITableViewCell *)[[sender superview] superview] ;

    for(UILabel *lbl in [currentCell.contentView subviews])
    {
        if(([lbl isKindOfClass:[UILabel class]]) && ([lbl tag] == 103))
        {
                str = [lbl.text intValue];

                str++;

                eventTicketList=(EventDetailTicketsList *)[eventTicketListArray objectAtIndex:row];
                eventTicketList.qty=str;

                lbl.text=[NSString stringWithFormat:@"%d",str];


        }
    }

        for(UIButton *btn in [currentCell.contentView subviews])
        {
            if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 105))
            {


                if (str==4) {
                    [btn setEnabled:NO];
                }
            }


        }

    for(UIButton *btn in [currentCell.contentView subviews])
    {
    if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 104))
    {

            [btn setEnabled:YES];




    }
    }
    if (row==3) {
        ticketTable.frame=CGRectMake(0, 0, 320, 70);


    }




    [currentCell setNeedsDisplay];



}

方法 - 按钮单击

-(void)DecreaseTickets:(id)sender{

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


    UITableViewCell *currentCell = (UITableViewCell *)[[sender superview] superview] ;

    for(UILabel *lbl in [currentCell.contentView subviews])
    {
        if(([lbl isKindOfClass:[UILabel class]]) && ([lbl tag] == 103))
        {

            str = [lbl.text intValue];

            str--;

            eventTicketList=(EventDetailTicketsList *)[eventTicketListArray objectAtIndex:row];
            eventTicketList.qty=str;


            lbl.text=[NSString stringWithFormat:@"%d",str];
        }
    }

    for(UIButton *btn in [currentCell.contentView subviews])
    {
        if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 104))
        {

            if (str<=0) {
                [btn setEnabled:NO];




            }
        }

    }
    for(UIButton *btn in [currentCell.contentView subviews])
    {
        if(([btn isKindOfClass:[UIButton class]]) && ([btn tag] == 105))
        {
            [btn setEnabled:YES];
        }

    }
         if (row==3){
        ticketTable.frame=CGRectMake(0, 0, 320, 200);
    }

    [currentCell setNeedsDisplay];



}
4

1 回答 1

0

在您的按钮单击中添加下面的代码,或者您也可以在didSelectRowForIndexPath.

[self.tableView beginUpdates];
[self.tableView endUpdates];

使用它会自动处理 tableview 调用下面的方法。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;// here you can calculate cell height according to your requirements.
}

希望对你有帮助...

于 2012-04-26T05:22:28.090 回答