难以向图像解释将有所帮助。首次加载UITableView时,detailText位于textLabel上方 - 如下:
但是当你稍微移动tableView时,它会自行纠正:
以下是我调整我觉得适用的所有内容的代码:
- (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;
}