Reputation: 2280
I have a tableview
but none of the cells are displaying longer text. What's even stranger is it doesn't seem to be a set value that they stop at. Some cells display more text than others before they are cut off. NSLog()
confirms the string is correctly set.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//code for getting string removed for readability
// Identifier for retrieving reusable cells.
static NSString *cellIdentifier = @"MyCellIdentifier";
// Attempt to request the reusable cell.
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// No cell available - create one.
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// format it
cell.textLabel.numberOfLines = 0;
cell.textLabel.font = [UIFont systemFontOfSize:12.0];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
}
// Set the text of the cell to the row index.
cell.textLabel.text = fulltext;
return cell;
}
I was under the impression the numberOfLines
function at 0
would handle it but all of the cells are at 2 lines, no more. How can I make them adjust to as many lines as needed? Thanks.
Upvotes: 3
Views: 1686
Reputation: 14068
I suggest to subclass UITableViewCell. Do not change much to cellForRowAtIndexPath but
cell = [[MyVeryOwnTableCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
assuming that your subclass of UITableViewCell
is MyVeryOwnTableCell
.
Within the implementation of MyVeryOwnTableCell you should overwrite the layoutSubViews method. First call [super layoutSubViews]
and then re-arrange the frame of the textLabel accordingly. You may not need to change the position but the height and the numberOfLines. Plus you may want to set an appropriate lineBreakMode. See the docs for details.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UILabel_Class/Reference/UILabel.html
Calculate the size of the text following this pattern:
CGSize textSize;
textSize = [textLabel.text sizeWithFont:... constraintToSize:
CGSizeMake(300.0f, 10000.0f) lineBreakMode: ...];
You do that within layoutSubViews of your cell implementation and you may have to use it in a similar way when you have to overwrite heightForRowAtIndexPath if the total size of the cell is too small.
The trick here is that at start you give size contraints exactly at the width of your UILabel (so the 300.0 may not be perfect for you) plus some more or less endless height. You get returned the actual size that the text requies (at a maximum of 10000 in this case). That height/size is then used to set the actual size of the label respectively to calculate the total height of the cell as required to display the text and probably other additional items.
Example:
textLabel setFrame:CGRectMake(10.0f, 10.0f, 300.0f, textSize.height);
Regarding the 300.0f. If you still want to use the auto-layout functionaltiy of UITableViewCell (which dynamically re-aligns the label in the event that an image is set) then replace it by textLabel.frame.size.width.
Upvotes: 1
Reputation: 2312
You have to adjust the height of each row at run time by calculating the height of cell label wrt string.
Following function gives u size of lable with fixed width of table view cell
- (CGSize) calculateLabelHeightWith:(CGFloat)width text:(NSString*)textString
{
CGSize maximumSize = CGSizeMake(width, 9999);
CGSize size = [textString sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Medium" size:14]
constrainedToSize:maximumSize
lineBreakMode:UILineBreakModeWordWrap];
return size;
}
use table cell height delegate method
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get String for corresponding Row
NSString *stringObject=fullText;
//here i ave taken row width 320
CGSize rowSize=[Utils calculateLabelHeightWith:320 text:stringObject];
return (rowSize.height+20);
}
this will adjust te height if row dynamically whenever row is created it will first adjust its height.
I hope this will solve ur problem.
Upvotes: 0
Reputation: 173
I think in order to make it show more lines, you'll have to adjust the frame size of cell.textlabel and cell itself. You have to make their frame size taller to fit the new text. If only two lines of text will fit, that's all it's going to show. You'll have to programmatically adjust the heights based on the lines needed.
Here's a link to a guy who shows how to make that frame size dynamic based on the number of lines.
Upvotes: 0