dynamic row height
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@""];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//cell.backgroundView = [[[CustomCell alloc] init] autorelease];
cell.selectedBackgroundView = [[[CustomCell alloc] init] autorelease];
// At end of function, right before return cell:
cell.textLabel.backgroundColor = [UIColor clearColor];
// Configure the cell.
UILabel *myLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(-30, 3, 300, 22)];
UILabel *myLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 22, 310, 200)];
UILabel *myLabel3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 170, 300, 20)];
Book开发者_JS百科 *aBook = [appDelegate.books objectAtIndex:indexPath.row];
myLabel1.text=aBook.title;
myLabel2.text=aBook.description;
myLabel3.text=aBook.pubDate;
NSString *desc = aBook.description;
if ([desc isEqualToString:nil]) {
NSLog(@"nullll lll ");
}
//myLabel2.textAlignment=UITextAlignmentCenter;
myLabel2.numberOfLines=5;
myLabel2.textColor=[UIColor blueColor];
myLabel2.lineBreakMode=UILineBreakModeWordWrap;
myLabel2.font=[UIFont systemFontOfSize:14];
myLabel3.numberOfLines=1;
myLabel1.numberOfLines=1;
myLabel1.textColor=[UIColor redColor];
myLabel1.font=[UIFont systemFontOfSize:20];
myLabel1.shadowColor=[UIColor redColor];
myLabel1.backgroundColor=[UIColor grayColor];
[cell.contentView addSubview:myLabel1];
[cell.contentView addSubview:myLabel2];
[cell.contentView addSubview:myLabel3];
hi guys! i have this following code, i want to display this from xml file. now its working but static row height. if the xml file dont have data then it leaves it as blank. so i want to change row height according to the data i provide using the mylabel1,2,3.text
As specified in the UITableViewDelegate protocol, you want to implement the tableView:heightForRowAtIndexPath:
method in your delegate. In that method, you're given the indexPath
, so you can repeat the line
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
Once you have the book, you can check its properties (looks like title
, description
, and pubDate
), then instead of adding them to the cell (which you do later), you return a calculated cell height based on the property values.
Hi There is one sollution try the method of NSString . write this is Height for cell
NSString *text1 = [arrOutline1 objectAtIndex:[indexPath row]];
CGSize constraint1 = CGSizeMake(CELL_CONTENT_WIDTH , 20000.0f);
CGSize size1 = [text1 sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint1 lineBreakMode:UILineBreakModeWordWrap];
CGFloat YPostion =MAX(size3.height, 44.0f);
cellHeight = cellHeight + YPostion + 20;;
return cellHeight ;
If I understand the question aright (and apologies if I don't), then:
- Implement the tableView:heightForRowAtIndexPath: method in your tableView delegate - get it to calculate and return a suitable height for the row;
- Configure the sizes (or just remove from the superview) the UILabelViews you are adding to the contentView. You do this in tableView:cellForRowAtIndexPath:
- Make sure you reload the table or, better, reload only the rows that have changed - this forces the tableView to fetch the height again
Here's an example of a tableView:heightForRowAtIndexPath method for a single section table that dynamically resizes the height of the cell row depending on how much text there is in the paragraph being displayed.
- (CGFloat) tableView: tableView heightForRowAtIndexPath: indexPath;
{
CGFloat rowHeight = [self rowHeight:[[self paragraphs] objectAtIndex: [indexPath row]];
return (rowHeight + EI_CELL_MARGIN);
}
And then a fragment from the tableView:cellForRowAtIndexPath: method.
NSString* cellText = [[self paragraphs] objectAtIndex: [indexPath row];
[[cell paragraphController] setText: cellText];
[[cell paragraphController] adjustFrameToTextSize];
(As it goes in this code I've subclassed UITableViewCell for convenience. The 'paragraphController' is just a ViewController with a view added to the cell's content view. The adjustFrameToTextSize method resets the view frame depending on the size of the content to display.)
精彩评论