2010-03-27 116 views
6

到目前为止,我用来创建自定义的笔尖,使我的单元格,因为我想,但这次,单元格的高度将从一个更改为另一个,所以我不能创建一个固定大小的细胞的笔尖。自定义单元格的表格视图(编程)

因此,我决定以编程方式创建它...下面的方式实现它的好方法?

// 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; 
} 

or ..我在这里错过了什么吗?原因到目前为止,它似乎不工作;)

感谢,

高堤耶。

回答

0

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

+0

请注意,[只有链接的答案](http://meta.stackoverflow.com/tags/link-only-answers/info)不鼓励,所以答案应该是搜索解决方案的终点(vs.而另一个引用的中途停留时间往往会随着时间推移而过时)。请考虑在此添加独立的摘要,并将链接保留为参考 – kleopatra 2015-09-18 07:13:55

0

为什么在不需要时创建标签?使用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; 
} 
相关问题