【问题标题】:Adding two text labels (one dynamic and one static) in the same table cell在同一个表格单元格中添加两个文本标签(一个动态和一个静态)
【发布时间】:2014-07-07 14:45:09
【问题描述】:

我在视图控制器中有一个带有自定义单元格的表格视图。我的 tableview 工作正常。我想要做的是以编程方式开发下面的图像。其中“标签”是自定义的文本,会根据某些输入而变化。如何包含这 2 个标签(在 cellForRowAtIndexPath 中:)并确定它们在单元格中的位置。 图片包含 2 个不同的表格单元格。

我知道如何通过情节提要执行此操作,但我需要以编程方式执行此操作,因为我在 xCode 4.5 中使用动态单元格。

图像指的是索引单元格 1 和 2。到目前为止,我只设法在每个单元格中包含一个文本标签。

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



static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:CellIdentifier];
}




    switch ([indexPath row])
    {
        case 0:
        {
           // image cell - image resize and centred


            UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(30,2, 180, 180)];
            imv.image=[UIImage imageNamed:@"test1.jpg"];


            imv.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
            [cell.contentView addSubview:imv];

            imv.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);

            cell.accessoryType = UITableViewCellAccessoryNone;

            break;
        }
        case 1:
        {


            cell.textLabel.text = @"Name";


            break;
        }

        case 2:
        {
            cell.textLabel.text = @"Manufacturer";

            break;
        }

        case 3:
        {
            cell.textLabel.text = @"Overall Score";

            break;
        }

        case 4:
        {
            cell.textLabel.text = @"Description";
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

            break;
        }

        case 5:
        {
            cell.textLabel.text = @"Videos";
            cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

            break;
        }
            }


    return cell;

}

提前谢谢你

【问题讨论】:

  • 您应该包含您尝试过的内容、代码等内容,以便人们知道您不知道/不了解的内容
  • 感谢您的回复。我添加了需要帮助的代码部分。 @believesInSanta
  • 可以接受其他人的回答或提供反馈吗?

标签: ios uitableview textlabel


【解决方案1】:

先检查默认的 UITableViewCell 样式:UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle.

如果可以使用它们,则不需要子类化 UITableViewCell。否则,您将不得不这样做并放下 3 个属性:一个 UIView 和 2 个 UILabel。原因是如果不使用默认单元格样式,则无法移动或添加单元格元素。

你的 UITableViewCell 子类应该有以下代码:

@interface UITableViewCellSubClass : UITableViewCell
@property (nonatomic, strong) UIView *view;
@property (nonatomic, strong) UILabel *label1;
@property (nonatomic, strong) UILabel *label2;
@end

@implementation UITableViewCellSubClass
@synthesize view;
@synthesize label1;
@synthesize label2;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    view = [[UIView alloc] initWithFrame:self.frame];
    [self addSubview:view];
    // initiate label1 with position (10,10,150,20)
    label1 = [[UILabel alloc] initWithFrame:CGRectMake(10,10,150,20)];
    // initiate label2 with position (170,10,150,20)
    label2 = [[UILabel alloc] initWithFrame:CGRectMake(170,10,150,20)];
    [view addSubview:label1];
    [view addSubview:label2];
}
return self;
}
@end

然后你可以在你的 cellForRowAtIndex: 方法中返回它,基本上:

UITableViewCellSubclass *cell = [[UITableViewCellSubclass alloc] initWithStyle:UITableViewCellDefault reuseIdentifier:@"cell"];
[cell.label1 setText:@"text"];
[cell.label2 setText:@"more text"];

希望这会有所帮助,不要忘记#import "UITableViewCellSubClass.h"

【讨论】:

  • 非常感谢。很抱歉接受迟到的回复,但我不在。再次感谢您
  • 我能再问你一些事情吗?如果我不希望所有单元格都具有相同的内容/tableviewcell 子类,那么通过 cellForRowAtIndex 最简单的方法是什么?
  • 您可以使用不同的 tableviewcell 子类,只要它们具有不同的重用标识符,或者您自己创建一个所有可能的 UILabel、UIButtons 等,其类型属性将决定位置等跨度>
  • 谢谢。为什么当我使用你的代码时它说 No visible @interface for 'UIView' 声明选择器 addSubView
  • 可能是因为你应该把它改成 addSubview
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-16
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
  • 2014-04-16
  • 1970-01-01
  • 2017-05-14
相关资源
最近更新 更多