问题
i want to display multiple lines in a Table's cell view for an IOS app that i'm working on. To be precise, i have a table view which will be populated with the JSON data that is returned from server, in here if the status returned is 0 or 2 (Status is one of the JSON object that is being returned for a profile) i have to display an error message that is of 5-6 lines.
Table's cell view is customized to display 2 UILabel's and obviously the height of this cell is small enough to display 2 labels. I found one solution, but it doesn't actually solve the problem. Can anyone suggest a different solution. If possible, link to an example will be a great help. Thanks
回答1:
What you're searching for is called "dynamic sized" cells. There is a tutorial on how to realize this : http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout
It does resize all cells height in function of the text length like in this example:
回答2:
if you have to show this multi lines message to one of the UILabels you currently have in UITableViewCell, then set the property "No of Lines" to 0 (Zero) and set "Line Mode" to UILineBreakModeWordWrap, Now for height of the cell you have two options i.e with contraints or without constraints, this will be done using cellForRowAtIndexPath method.
1.if (error_message_shown) {
return 200.0;
else
return 60.0;
With constraints
Add top, bottom, left, right, width and height constraints, ensure to use >= for height and calculate the height of the text assigned to the cell using below methodCGSize size = [string sizeWithAttributes: @{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];
回答3:
When I have dynamic cell height I use something like
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
static MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"my_cell_id"];
NSDictionary *responseItem = [self itemFor:indexPath];
return (CGFloat)[cell heightFor:responseItem];
}
来源:https://stackoverflow.com/questions/26571033/display-multiple-lines-in-a-table-views-cell-ios