'UITableViewCell'没有显示标签

时间:2016-03-04 14:02:52

标签: ios objective-c uitableview

这是我的代码

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

    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}

    self.credits = (UILabel*)[cell viewWithTag:101];
    self.credits.text = @"Available Credits";
        cell.backgroundColor = [UIColor redColor];
    self.credits.textColor = [UIColor blackColor];
    [cell.contentView addSubview:self.credits];

信用是这里的标签

1 个答案:

答案 0 :(得分:0)

您在UIViewController中存储了一个小区标签,然后在内容视图中尝试addSubview

正确的做法应该是:

  1. 创建UITableViewCell
  2. 的子类
  3. 在此单元格内指定一个标签作为属性(可以通过代码,通过xib)
  4. cellForRowAtIndexPath:中使用该属性名称指定Available Credits
  5. 你的cellForRowAtIndexPath最终应该是这样的:

    - (MyCustomUITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        MyCustomUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        self.myNewProperty.text = @"Available Credits";
    }