【问题标题】:Add detail text on label of custom UITableViewCell在自定义 UITableViewCell 的标签上添加详细信息
【发布时间】:2015-01-28 06:11:20
【问题描述】:

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

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

我添加了新的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];

在我的情况下,我无法使用此代码。

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

以下是 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;
}

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

【问题讨论】:

  • 去自定义表格视图单元格,为什么你有任何特定的需求?
  • 在您填充标签的位置显示代码。
  • 我使用自定义表格视图单元格。需要知道如何在那里为标签添加详细文本。
  • @FawadMasud: cell.settingLabel.text = @"账户信息"; cell.subText.text = @"显示您的家庭企业帐户信息" in cellForRowAtIndexPath
  • 你在使用自动布局吗?如果是这样,您的约束设置是否正确?

标签: ios objective-c iphone xcode


【解决方案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;

【讨论】:

  • 这是每个人都建议的,所以我这样做了。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
  • 2011-05-23
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2017-09-12
相关资源
最近更新 更多