更改UItableview的大小会更改自定义单元格的功能

时间:2012-04-26 05:18:34

标签: iphone uitableview

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

enter image description here

在此" +"标志增加了票数和" - "减少它。当票数为0时," - "被禁用,当票数为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];



}

1 个答案:

答案 0 :(得分: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.
}

希望,这会对你有帮助......