【问题标题】:Adding a property to UITableViewCell向 UITableViewCell 添加属性
【发布时间】:2013-02-02 20:03:29
【问题描述】:

我正在尝试在 UIViewController 中创建 UITableView。我也想创建一些自定义 UITableViewCell。但是,我在试图弄清楚如何将 UILabel 属性添加到 UITableViewCell 时遇到了一些困难。运行代码时出现以下错误...“在'UITableViewCell'类型的对象上找不到属性'_label1'”任何帮助将不胜感激。

.h

@interface gideViewController : UIViewController <UITableViewDelegate,     UITableViewDataSource> {
}
@property (nonatomic, strong) IBOutlet UILabel *label1;
@property (nonatomic, strong) NSArray *data;

.m

@synthesize label1 = _label1;
@synthesize data = _data;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self._data count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:     (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"TableCell";

UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell     alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
}

//*********** This is where I get the compiler error ... "Property '_label1' not found on object of type 'UITableViewCell'"///
cell._label1.text = [self._data
                       objectAtIndex: [indexPath row]];

return cell;
}

- (void)viewDidLoad{
[super viewDidLoad];
self.data = [[NSArray alloc]
                 initWithObjects:@"ABC",
                 @"DEF",
                 @"XYZ",
                 @"JKY", nil];
}

【问题讨论】:

    标签: objective-c xcode properties uitableview uiviewcontroller


    【解决方案1】:

    您在哪里创建自定义表格视图单元格?你把它子类化了吗?如果您确实调用了它并将您的子类命名为 MyTableViewCell 然后更改该行

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
    

    MyTableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
    

    并使用您要在此处使用的单元格标识符注册 MyTableViewCell。 无论如何,您可能希望为这些类型的单元格使用另一个标识符。

    static NSString *CellIdentifier = @"MyTableCell";
    

    另一个选项是在创建时将label1 作为子视图(不是属性!!!)添加到每个单元格。

    if (cell == nil) {
        // Caution! If you want this line to be executed, then you MUST not register the cell identifier with any cell's class. Because if you register it then the dequeue method will automatically create an object of that class and return it. 
        // Plus you must not create this cell in IB using storyboard because then it is atuomatically registered. (Or do it properly and completely in IB. But then this code sniplet would be redundant) 
        cell = [[UITableViewCell     alloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];
        UILabel *label1 = [[UILabel alloc] init];  // I think you could use initWithFrame too for layouting purposes
        [cell.contentview addSubview:label1];
        lable1.tag = 99;   // this is to fetch the label on re-use
        // here you may layout the label, set colors etc.
    }
    // here you can fetch the label using the tag as identifier and then set its text value
    

    当然,还有更多的实现方式。但是,子类化可能是更明智的选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 2020-05-11
      • 2017-08-26
      相关资源
      最近更新 更多