问题描述:

My UITableview is displaying the data, but the text in one section is moving out of the box and displaying in other section..i don't know why?

Here is my code:

if(indexPath.section == 0){

cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

cell.textLabel.textColor = [UIColor blackColor];

cell.detailTextLabel.numberOfLines=3;

cell.detailTextLabel.textColor=[UIColor colorWithRed:0.40 green:0.40 blue:0.40 alpha:1.0];

cell.textLabel.text=[NSString stringWithFormat:@"School Hours:"];

cell.detailTextLabel.text=[NSString stringWithFormat:@"Take in time: %@ \nDissmiss time:%@ \nPhone No: %@ ",[array objectAtIndex:indexPath.row],[array1 objectAtIndex:indexPath.row],[array2 objectAtIndex:indexPath.row]];

cell.textLabel.backgroundColor=[UIColor clearColor];

cell.detailTextLabel.backgroundColor=[UIColor clearColor];

}

else if(indexPath.section == 1){cell.textLabel.font = [UIFont boldSystemFontOfSize:14];

cell.textLabel.textColor = [UIColor blackColor];

cell.detailTextLabel.numberOfLines=3;

cell.detailTextLabel.textColor=[UIColor colorWithRed:0.40 green:0.40 blue:0.40 alpha:1.0];

cell.textLabel.text=[NSString stringWithFormat:@"Principal"];

NSString *thumbs = [NSString stringWithFormat:@"%@", [imagearray objectAtIndex:indexPath.row]];

UIImage *thumbs1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:

[NSURL URLWithString:thumbs]]];

// cell.imageView.image = [UIImage imageNamed:th];

cell.detailTextLabel.text=[NSString stringWithFormat:@" %@ ",[imagearray objectAtIndex:indexPath.row]] ;

cell.textLabel.backgroundColor=[UIColor clearColor];

cell.detailTextLabel.backgroundColor=[UIColor clearColor];}

Can any one let me know what went wrong. i even set header and footer for the sections.

网友答案:

set the height of cell For example...

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

    NSString *titleString = yourTitleString;///here just write your string here
    NSString *detailString = [NSString stringWithFormat:@"Take in time: %@ \nDissmiss time:%@ \nPhone No: %@ ",[array objectAtIndex:indexPath.row],[array1 objectAtIndex:indexPath.row],[array2 objectAtIndex:indexPath.row]];
    CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
    CGSize detailSize = [detailString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

    return detailSize.height+titleSize.height;
}

And i just set the code for the common cell for Dynamic height of cell see bellow code of cellForRowAtIndexPath method...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = @"YourTitle";
    cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
    cell.textLabel.numberOfLines = ceilf([@"YourTitle" sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0);
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Take in time: %@ \nDissmiss time:%@ \nPhone No: %@ ",[array objectAtIndex:indexPath.row],[array1 objectAtIndex:indexPath.row],[array2 objectAtIndex:indexPath.row]];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.numberOfLines = ceilf([[NSString stringWithFormat:@"Take in time: %@ \nDissmiss time:%@ \nPhone No: %@ ",[array objectAtIndex:indexPath.row],[array1 objectAtIndex:indexPath.row],[array2 objectAtIndex:indexPath.row]]; sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0);

    return cell;
}
网友答案:

For such a huge data you need to truncate the data to display it. For this you can use:

[[cell detailTextLabel] setLineBreakMode:UILineBreakModeTailTruncation];
 cell.detailTextLabel.numberOfLines = 0;

Another option is you need to adjust your tableViewCell's height using the heightForRowAtIndexPath delegate.

相关阅读:
Top