【问题标题】:Table view with custom cell (programmatically)带有自定义单元格的表格视图(以编程方式)
【发布时间】:2011-02-01 12:24:23
【问题描述】:

到目前为止,我过去常常创建自定义笔尖来制作我想要的单元格,但这次,一个单元格的高度会从一个变为另一个,因此我无法创建一个固定大小的单元格的笔尖。

所以我决定以编程方式创建它...下面的方法是实现它的好方法吗?

// Customize the appearance of table view cells.
- (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] autorelease];
        UILabel *pseudoAndDate = [[UILabel alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,20.0)];
        [pseudoAndDate setTag:1];
        [cell addSubview:pseudoAndDate];
        [pseudoAndDate release];
    }

    CommentRecord *thisRecord = [comments objectAtIndex:[indexPath row]];

    UILabel *label = (UILabel *)[cell viewWithTag:1];
    [label setText:[NSString stringWithFormat:@"%@ | %@",thisRecord.author,thisRecord.date]];

    return cell;
}

或者..我在这里错过了什么吗?因为到目前为止它似乎不起作用;)

谢谢,

哥蒂。

【问题讨论】:

    标签: uitableview row customization cell reusability


    【解决方案1】:

    以编程方式自定义 UITableViewCell 的新链接Apple Documentation UITableViewCell

    【讨论】:

    • 请注意 link-only answers 是不鼓励的,所以答案应该是寻找解决方案的终点(与另一个中途停留的参考相比,随着时间的推移往往会变得陈旧)。请考虑在此处添加独立的概要,保留链接作为参考
    【解决方案2】:

    【讨论】:

      【解决方案3】:

      为什么在不需要时创建标签?使用 UITableViewCell 的标签。

      - (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] autorelease];
          }
      
          CommentRecord *thisRecord = [comments objectAtIndex:[indexPath row]];
      
          cell.textLabel.text = [NSString stringWithFormat:@"%@ | %@",thisRecord.author,thisRecord.date];
      
          return cell;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-06
        • 1970-01-01
        相关资源
        最近更新 更多