1

UITableview正在显示数据,但是一个部分中的文本正在移出框并显示在其他部分中..我不知道为什么?

在此处输入图像描述

这是我的代码:

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];}

任何人都可以让我知道出了什么问题。我什至为这些部分设置了页眉和页脚。

4

2 回答 2

0

设置单元格的高度例如...

    - (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;
}

我只是为单元格的动态高度设置了通用单元格的代码,请参见下面的cellForRowAtIndexPath方法代码...

- (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;
}
于 2012-12-26T10:31:30.127 回答
0

对于如此庞大的数据,您需要截断数据以显示它。为此,您可以使用:

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

另一种选择是您需要使用heightForRowAtIndexPath委托调整 tableViewCell 的高度。

于 2012-12-26T10:48:47.667 回答