在自定义 UITableViewCell 的标签上添加详细文本
Add detail text on label of custom UITableViewCell
这里还有其他类似的问题,但其中 none 回答了我正在寻找的问题。
我的设置屏幕应该如下所示:
我添加了新的 UITableViewCell
class 并使用以下代码将其连接到主 table:
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];
}
我知道我们将 table 样式设置为 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;
}
我需要知道如何添加只有一个标签的详细文本。
为什么需要自定义单元格?只需使用普通的 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;
这里还有其他类似的问题,但其中 none 回答了我正在寻找的问题。
我的设置屏幕应该如下所示:
我添加了新的 UITableViewCell
class 并使用以下代码将其连接到主 table:
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];
}
我知道我们将 table 样式设置为 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;
}
我需要知道如何添加只有一个标签的详细文本。
为什么需要自定义单元格?只需使用普通的 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;