简体   繁体   English

如何在TableView ipad中动态更改行高

[英]how to change height of row in tableview ipad dynamically

hi I am new to iPad developing. 嗨,我是iPad开发的新手。 So I am using web service to display data in tableview. 所以我正在使用Web服务在tableview中显示数据。 So how can I change the row heigh of each cell I tried but it showing more height for that one? 那么,如何更改我尝试过的每个单元格的行高,但显示出该单元格的更多高度? I used three arrays for displaying data in tableview So if I give more data to array through web Service. 我使用了三个数组在tableview中显示数据,因此,如果我通过Web Service将更多数据提供给数组。 So how can I change for every row for table view. 因此,如何更改表视图的每一行。 Can anyone help me ? 谁能帮我 ?

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

You can probably try out the delegate method: 您可能可以尝试使用委托方法:

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

And specify the height of the cell parameter based on indexpath. 并根据indexpath指定单元格参数的高度。

You can change UITableViewCell height in this delegate - 您可以在此委托中更改UITableViewCell高度-

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

For this to work, you could dynamically figure out the content height on the fly or have some predefined heights 100,150,200 etc. & based on some cutoff on your data, use one of these heights... Will reduce complexity. 为此,您可以动态地动态计算出内容的高度,或者具有一些预定义的高度100、150、200等。并且根据您的数据的某些临界值,使用这些高度之一...将降低复杂性。

Otherwise you might want to find the length 否则,您可能想要找到长度


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

This is what I used in my project and it worked perfectly well. 这就是我在项目中使用的工具,效果很好。 Change the values according to your requirements. 根据您的要求更改值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM