How to increase the height of the cell in a tableview in iphone?

百般思念 提交于 2019-12-04 04:47:53

问题


I am new to iphone development.I am parsing a xml file and displaying the title, date, view and summary in each row of a table.The contents of summar is big ,so only first 3 words are displayed in the cell.How can i increase the height of the row with respect to the length of the contents.All the content should fit properly inside the cell and full content should be displayed.Please help me out.Thanks.


回答1:


You can do this in your tableView.delegate/datasource:

- (CGFloat)tableView:(UITableView *)aTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   switch (indexPath.row){
       case 0:
         if(indexPath.section==0)
            return 123.0; // first row is 123pt high
       default:
         return 40.0; // all other rows are 40pt high 
   }
}

This does however slow down scroll performance. Ideally you should set the row height for all rows to be identical using tableview.rowHeight.




回答2:


(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *text = @"write your dynamic text here";
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
    CGSize constraintSize = CGSizeMake(320.0f, 999);  // Make changes in width as per your label requirement. 

    CGSize textSize = [text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return  textSize.height;
}



回答3:


if you have multiple tableview in one view controller, you can do for each different table view as a below method.

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

    if (tableView == self.tableViewCampaigns) {

        return 45;
    }else if(tableView == self.tableViewAgenda)
        return 32;
    else
        return 29;
}



回答4:


One way to do it is to manually set the size of the cell. Just replace ROW_HEIGHT with something suitable.

cell.frame = CGRectMake(0.0, 0.0, 320.0, ROW_HEIGHT);



回答5:


Try using this Delegates which automatically manages according to the content of the cell.

 func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
            return 100
        }

        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableViewAutomaticDimension
        }


来源:https://stackoverflow.com/questions/2252628/how-to-increase-the-height-of-the-cell-in-a-tableview-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!