在自定义UITableViewCell的标签上添加详细文本

时间:2015-01-28 06:11:21

标签: ios objective-c iphone xcode

这里还有其他类似的问题,但没有一个能回答我正在寻找的问题。

我的设置屏幕应如下所示:

enter image description here

我添加了新的UITableViewCell类,并使用以下代码将其连接到主表:

static NSString *cellIdentifier = @"settingCellIdentifier";
SettingsTableViewCell *cell = (SettingsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil){

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SettingsTableViewCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

我知道我们将表格样式设置为UITableViewCellStyleSubtitle以添加详细文本,但我知道这样做的唯一方法是:

cell = [[UITableViewCell alloc]
            initWithStyle:UITableViewCellStyleSubtitle
            reuseIdentifier:SimpleTableIdentifier];

我无法在我的案例中使用此代码。

目前我使用了两个标签,它带来了以下混乱:

enter image description here

以下是cellForRowAtIndexPath代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
static NSString *cellIdentifier = @"settingCellIdentifier";
SettingsTableViewCell *cell = (SettingsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if (cell == nil){

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SettingsTableViewCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}

NSInteger row = indexPath.row;


switch (row) {
    case 0:
        cell.settingLabel.text = @"Settings"; 
        cell.subText.text = @"";
        break;
    case 1:
        cell.settingLabel.text = @"Account Info";
        cell.subText.text = @"Display your Home Business account information";
        break;
    case 2:
        cell.settingLabel.text = @"Sign Out";
        cell.subText.text = @"Signed in as ";
        break;
    case 3:
        cell.settingLabel.text = @"Send Feedback";
        cell.subText.text = @"Send comments, requests or error reports to Home Business";
        break;
    case 4:
        cell.settingLabel.text = @"Terms of User";
        cell.subText.text = @"";
        break;
    case 5:
        cell.settingLabel.text = @"Privacy Policy";
        cell.subText.text = @"Your privacy and security are our top priorities";
        break;
    case 6:
        cell.settingLabel.text = @"Like us on Facebook";
        cell.subText.text = @"";
        break;
    case 7:
        cell.settingLabel.text = @"Follow us on Twitter";
        cell.subText.text = @""; 
        break;
    case 8:
        cell.settingLabel.text = @"About Us";
        cell.subText.text = @"";
        break;

    default:
        break;
}

return cell;
}

我需要知道如何只用一个标签添加详细文本。

1 个答案:

答案 0 :(得分:1)

为什么需要自定义单元格?只需使用普通的tableview单元格及其textLabel& detailTextLabel。这将满足您的需求。您甚至可以为detailTextLabel设置行数。试试这个:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"Profile";
        cell.detailTextLabel.text = @"Lorem ipsum dolor sit amet, vix id debet libris. Quem oblique blandit ne vix, repudiare prodesset an mel. At libris tacimates accusata has, eum at ignota cotidieque. Pro malorum nominati ei.";
    }
    else if (indexPath.row == 1){
        cell.textLabel.text = @"Server";
        cell.detailTextLabel.text = @"Lorem ipsum dolor sit amet, vix id debet libris. Quem oblique blandit ne vix, repudiare prodesset an mel. At libris tacimates accusata has, eum at ignota cotidieque. Pro malorum nominati ei.";
    }
    else if (indexPath.row == 2){
        cell.textLabel.text = @"Security";
        cell.detailTextLabel.text = @"Lorem ipsum dolor sit amet, vix id debet libris. Quem oblique blandit ne vix, repudiare prodesset an mel. At libris tacimates accusata has, eum at ignota cotidieque. Pro malorum nominati ei.";
    }
    else if (indexPath.row == 3){
        cell.textLabel.text = @"Forget Password";
        cell.detailTextLabel.text = @"Lorem ipsum dolor sit amet, vix id debet libris. Quem oblique blandit ne vix, repudiare prodesset an mel. At libris tacimates accusata has, eum at ignota cotidieque. Pro malorum nominati ei.";
    }
    else if (indexPath.row == 4){
        cell.textLabel.text = @"About";
        cell.detailTextLabel.text = @"Lorem ipsum dolor sit amet, vix id debet libris. Quem oblique blandit ne vix, repudiare prodesset an mel. At libris tacimates accusata has, eum at ignota cotidieque. Pro malorum nominati ei.";
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    return cell;