在UITableViewCell中加载错误空间的DetailText

时间:2014-12-30 16:19:26

标签: ios objective-c uitableview

难以向图像解释将有所帮助。首次加载UITableView时,detailText位于textLabel上方 - 如下:

enter image description here

但是当你稍微移动tableView时,它会自行纠正:

enter image description here

以下是我调整我觉得适用的所有内容的代码:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.detailTextLabel.numberOfLines = 0;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }


    if (indexPath.row == 0){
    cell.textLabel.text = @"Two";
        cell.detailTextLabel.text = [self.rules objectForKey:@"two"];
    }
    if (indexPath.row == 1){
        cell.textLabel.text = @"Three";
    }
    if (indexPath.row == 2){
        cell.textLabel.text = @"Four";
    }
    if (indexPath.row == 3){
        cell.textLabel.text = @"Five";
    }
    if (indexPath.row == 4){
        cell.textLabel.text = @"Six";
    }
    if (indexPath.row == 5){
        cell.textLabel.text = @"Seven";
    }
    if (indexPath.row == 6){
        cell.textLabel.text = @"Eight";
    }
    if (indexPath.row == 7){
        cell.textLabel.text = @"Nine";
    }
    if (indexPath.row == 8){
        cell.textLabel.text = @"Ten";
    }
    if (indexPath.row == 9){
        cell.textLabel.text = @"Jack";
    }
    if (indexPath.row == 10){
        cell.textLabel.text = @"Queen";
    }
    if (indexPath.row == 11){
        cell.textLabel.text = @"King";
    }
    if (indexPath.row == 12){
        cell.textLabel.text = @"Ace";
    }
    CGRect frame = tableView.frame;

    UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width-70, 10, 50, 30)];
    [addButton setTitle:@"Get" forState:UIControlStateNormal];
 addButton.titleLabel.font = [UIFont fontWithName:@"Avenir-Black" size:14.0f];
    addButton.backgroundColor = [UIColor redColor];
    [cell.contentView addSubview:addButton];

    cell.detailTextLabel.numberOfLines = 0;
    cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
        [cell.detailTextLabel sizeToFit];

    return cell;
}

行高:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *str = [self.rules objectForKey:@"two"];
    CGSize size = [str sizeWithFont:[UIFont fontWithName:@"Avenir" size:17] constrainedToSize:CGSizeMake(280, 999) lineBreakMode:NSLineBreakByWordWrapping];
    return size.height + 40;
}

0 个答案:

没有答案