I have two UILabel
in side of UICell
, which contains dynamic text so i need to resize its frame according to content, for which im using [string sizeWithFont]
method to calculate the height of label view's frame inside of TableView
heightForRowAtIndexPath
to set the height of cell according to label height. Now what the problem is when i scroll my table, label inside of cell starts to shrink if i remove [label sizeToFit]
method it doesn't shrink but from that my labels are getting overlapped which looks very messy. Where i am wrong please guide me..
here is my code for cellRowAtIndexPath
method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cell");
BOOL ente = FALSE;
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
ente = TRUE;
break;
}
}
}
/*
[cell.title sizeToFit];
[cell.dataTime sizeToFit];
CGSize size1 = [[mainEventArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(275, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
cell.title.frame = CGRectMake(50, 6, size1.width, size1.height);
CGSize size2 = [[mainEventTimeArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:11.0f] constrainedToSize:CGSizeMake(275, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
cell.dataTime.frame = CGRectMake(50, 24, size2.width, size2.height);
*/
cell.title.text = [mainEventArray objectAtIndex:[indexPath row]];
cell.dataTime.text = [mainEventTimeArray objectAtIndex:[indexPath row]];
//if (ente) {
// NSLog(@"helllloo");
[cell.title sizeToFit];
[cell.dataTime sizeToFit];
//}
return cell;
}
and for heightForRowAtIndexPath
method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CustomCell";
CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
{
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (CustomCell *) currentObject;
break;
}
}
}
}
CGSize size1 = [[mainEventArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(230, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
cell.title.frame = CGRectMake(0, 0, size1.width, size1.height);
CGSize size2 = [[mainEventTimeArray objectAtIndex:[indexPath row]] sizeWithFont:[UIFont systemFontOfSize:11.0f] constrainedToSize:CGSizeMake(230, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
cell.dataTime.frame = CGRectMake(0, 0, size2.width, size2.height);
//NSLog(@"%f", size1.height + size2.height + 20);
return size1.height + size2.height + 20;
//NSString *str = [heights_ objectAtIndex:[indexPath row]];
// return [str floatValue] ;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…