如何动态更改tableview ipad中行的高度

时间:2011-08-09 09:01:45

标签: iphone ios objective-c ipad uitableview

嗨,我是iPad新手。所以我使用Web服务在tableview中显示数据。那么如何更改我尝试的每个单元格的行高,但是它显示了更高的高度? 我使用三个数组在tableview中显示数据所以如果我通过Web服务向数组提供更多数据。那么如何更改表格视图的每一行。任何人都可以帮助我吗?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [arrOutline1 count] + [arrOutline2 count];           
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{       
    if ((indexPath.row)%2==0) {
        return 100;         
    }
    else {          
         //strData3 =[[NSMutableString alloc]init];
        NSLog(@" the data is %@",[arrOutline3 objectAtIndex:countRow]);

        //strData3 = [NSMutableString stringWithFormat:@"%@",[arrOutline3 objectAtIndex:countRow]];
      int myLength = [[NSMutableString stringWithFormat:@"%@",[arrOutline3 objectAtIndex:countRow]] length]; 
        NSLog(@" the length of dat is :%d",myLength);

        int LengthCell;
            int LengthCell = myLength /3;
            CGFloat heightCell3 ;
        int l = LengthCell * 19;
        heightCell3 =(CGFloat )l;
        NSLog(@" the heigh of cell is :%f",heightCell3 );
        countRow = countRow + 1;
        return heightCell3;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        static NSString *CellIdentifier = @"Cell";

        if ((indexPath.row)%2==0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

            txtlable1= [[UILabel alloc]initWithFrame:CGRectMake(50, 0, W, H)]; 
            txtlable1.backgroundColor=[UIColor clearColor];
            txtlable1.font=[UIFont fontWithName:@"Arial" size:25.0];
            txtlable1.text =[arrOutline1 objectAtIndex:count2];
            txtlable1.lineBreakMode = UILineBreakModeWordWrap;
            txtlable1.textAlignment = UITextAlignmentLeft;
            txtlable1.numberOfLines = 0;
            [[cell contentView]addSubview:txtlable1];
            [txtlable1 release];            

        txtlable2= [[UILabel alloc]initWithFrame:CGRectMake(600, 0, W-200, H)]; 
            txtlable2.backgroundColor=[UIColor clearColor];
            txtlable2.font=[UIFont fontWithName:@"Arial" size:20.0];
            txtlable2.textAlignment = UITextAlignmentLeft;
            txtlable2.lineBreakMode = UILineBreakModeWordWrap;
            txtlable2.numberOfLines = 0;
            txtlable2.text =[arrOutline2 objectAtIndex:count2];
            [[cell contentView]addSubview:txtlable2];
            count2 =count2+1;
            [txtlable2 release];
            return cell;

        }
    else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

        int myLength = [[NSMutableString stringWithFormat:@"%@",[arrOutline3 objectAtIndex:count1]] length]; 
        NSLog(@" the length of dat is :%d",myLength);
        int LengthCell = myLength /3;
        CGFloat heightCell3 ;
        int l = LengthCell * 15;
        heightCell3 =(CGFloat )l;
        NSLog(@" the heigh of cell is :%f",heightCell3 );

        txtlable3=[[UILabel alloc]initWithFrame:CGRectMake(50,0,W+400,heightCell3)];  
        txtlable3.backgroundColor=[UIColor clearColor];

        txtlable3.font=[UIFont fontWithName:@"Arial" size:30.0];
        txtlable3.lineBreakMode = UILineBreakModeWordWrap;
        txtlable3.numberOfLines = 0;

        txtlable3.text =[arrOutline3 objectAtIndex:count1];
        [cell addSubview:txtlable3];
        count1 = count1 + 1;

        return cell;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    arrOutline1 = [[NSMutableArray alloc]initWithObjects:@"vijay sankar ramamknkakjdkl.\n the thinking the main. candidate of things  thisisingamaldaj",@"Amith",@"Gupta",@"RightData",@"RightData1",@"RightData2",@"RightData3",@"RightData4",@"RightData5",@"RightData6",nil];
    arrOutline2 = [[NSMutableArray alloc]initWithObjects:@"ganesh",@"surya",@"sudha",@"LeftData",@"LeftData1",@"LeftData2",@"LeftData3",@"LeftData4",@"LeftData5",@"LeftData6",nil];
    arrOutline3 = [[NSMutableArray alloc]initWithObjects:@"vijay sankar ramamknkakjdkl.\n the thinking the main.\n candidate of things  thisisingamaldaj .\n ",@"jagadish",@"night fhdfhda fhadfhadf.\n dfafhdjkahfa",@"3rdData",@"3rdData1 .\n vijay sankar ramamknkakjdkl.\n the thinking the main.\n candidate of things  thisisingamaldaj",@"3rdData2",@"3rdData3",@"3rdData4",@"3rdData5",@"3rdData6 3rdData1 .\n vijay sankar ramamknkakjdkl.\n the thinking the main.3rdData1 .\n vijay sankar ramamknkakjdkl.\n the thinking the main.3rdData1 .\n vijay sankar ramamknkakjdkl.\n the thinking the main.",nil];

    CGFloat heihtTable;
    heihtTable =[arrOutline1 count] + [arrOutline3 count] ;
    heihtTable =heihtTable *175 ;
    scrl.contentSize=CGSizeMake(728,heihtTable);
    count1=0;
    count2=0;
    countRow =0;
        lblCompetency = [[UILabel alloc]initWithFrame:CGRectMake(20, 50, 400, 100)];
    lblCompetency.text = @"Competency";
    lblCompetency.font = [UIFont fontWithName:@"Helvetica" size: 24.0];
    [self.scrl addSubview:lblCompetency];

    NSLog(@"The height is %f",heihtTable);
    TableOutline = [[UITableView alloc] initWithFrame:CGRectMake(10, 150,1000,heihtTable) style:UITableViewStyleGrouped];
    TableOutline.separatorColor=[UIColor blackColor];
    TableOutline.backgroundColor=[UIColor clearColor];
    [TableOutline setUserInteractionEnabled:NO];
    TableOutline.opaque=YES;
    TableOutline.delegate =self;
    TableOutline.dataSource = self;
    TableOutline.allowsSelection=FALSE;

    UIView *viewTable2=[[UIView alloc]initWithFrame:CGRectMake(10, 200, 1000, heihtTable)];
    viewTable2.backgroundColor=[UIColor clearColor];
    TableOutline.backgroundView =viewTable2;
    [self.scrl addSubview:TableOutline];    
}

3 个答案:

答案 0 :(得分:2)

您可以尝试委托方法:

    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath

并根据索引路径指定单元格参数的高度。

答案 1 :(得分:2)

您可以在此委托中更改UITableViewCell高度 -

- (CGFloat)tableView:(UITableView *)tableView 
heightForRowAtIndexPath:(NSIndexPath *)indexPath
{ }

为了使其正常工作,您可以动态地计算出内容高度,或者具有一些预定义的高度100,150,200等。&根据您的数据的某些截止,使用其中一个高度...将降低复杂性。

否则你可能想找到长度


答案 2 :(得分:1)

- (CGFloat) tableView: (UITableView *) tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath
{
    CGSize labelSize = CGSizeMake(200.0, 20.0);
    NSString *strTemp = [arrServices objectAtIndex:indexPath.row];
    if ([strTemp length] > 0)
        labelSize = [strTemp sizeWithFont: [UIFont boldSystemFontOfSize: 14.0] constrainedToSize: CGSizeMake(labelSize.width, 1000) lineBreakMode: UILineBreakModeWordWrap];
    return (labelSize.height + 10);
}

这是我在我的项目中使用的,它运作得非常好。根据您的要求更改值。