点击后我的自定义单元格搞砸了?

时间:2012-08-28 19:20:04

标签: iphone uiview label tableview cell

之前:http://tinypic.com/view.php?pic=2j6a4h4&s=6 之后:http://tinypic.com/view.php?pic=demxi&s=6

单击后,我在listView中的单元格布局出现问题。正如你在前后图片中看到的那样,我单元格中的3个标签(名称,书籍,章节)都搞砸了?我在代码中遗漏了什么? /问候

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

if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Bookmark *item = [self.items objectAtIndex:indexPath.row];

NSArray *chunks = [item.name componentsSeparatedByString: @","];

NSString *name;
NSString *book;
NSString *chapter;

if ([chunks count] > 0)
{
    name = [chunks objectAtIndex:0];
    if ([chunks count] > 1)
    {
        book = [chunks objectAtIndex:1];
        if ([chunks count] > 2)
        {
            chapter = [chunks objectAtIndex:2];
        }
    }
}

UIView * pNewContentView= [[UIView alloc] initWithFrame:cell.contentView.bounds];
CGRect labelFrame= pNewContentView.bounds;
labelFrame.size.height= labelFrame.size.height * 0.5;

UILabel* pLabel1=[[UILabel alloc] initWithFrame:labelFrame];
[pNewContentView addSubview:pLabel1];

labelFrame.origin.y= labelFrame.size.height;
UILabel* pLabel2=[[UILabel alloc] initWithFrame:labelFrame];
[pNewContentView addSubview:pLabel2];

labelFrame.origin.y= labelFrame.origin.y + labelFrame.size.height;
UILabel* pLabel3=[[UILabel alloc] initWithFrame:labelFrame];
[pNewContentView addSubview:pLabel3];

[cell.contentView addSubview:pNewContentView];

[pLabel1 setText:(name)];    
[pLabel2 setText:(book)];  
[pLabel3 setText:(chapter)];       




return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 70; // height of tableView Cell
}

1 个答案:

答案 0 :(得分:1)

只是扩展@onnoweb所说的内容并将其移至答案部分,因为他是对的:

正在发生的事情是每次细胞刷新,你要为每个细胞添加额外的3个标签,造成真正的坏记忆。油漆问题是由于新标签模糊了原因导致所有标签默认都有白色BG。

您希望将标签创建代码移动到初始化新创建的单元格的位置,但是存在一个问题,即在现有单元格中没有优雅的方式来访问添加的标签。我的建议是定制单元格,比如这个,总是创建一个UITableViewCell的自定义子类。 (我个人认为这是最优雅的方法):

代表: UIMyCustomTableViewCell.h

@interface UIMyCustomTableViewCell : UITableViewCell {

}
@property (nonatomic, retain) UILabel *label1;
@property (nonatomic, retain) UILabel *label2;
@property (nonatomic, retain) UILabel *label3;

@end

UIMyCustomTableViewCell.m

@implementation UIMyCustomTableViewCell
@synthesize label1 = _label1;
@synthesize label1 = _label2;
@synthesize label1 = _label3;

... init, memory cleanup, etc.

@end

此时,您可以在cellForRowAtIndexPath代码中使用该单元格:

if (cell == nil)
{
    cell = [[UIMyCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    CGRect labelFrame = cell.bounds;
    labelFrame.size.height= labelFrame.size.height * 0.5;

    cell.label1 = [[UILabel alloc] initWithFrame:labelFrame];
    etc...
}

cell.label1.text = ...
cell.label2.text = ...
cell.label3.text = ...

我也非常喜欢在xib文件中设置所有自定义单元格的外观和感觉,而不是在上面的代码中初始化标签,在相当大的应用程序中更清晰。

希望这有帮助。