以编程方式实例化子类uitableviewcell initwithstyle不显示分隔符

时间:2012-05-23 03:39:00

标签: objective-c ios cocoa-touch

在iOS5.0中我试图用initwithstyle以编程方式实例化一个子类化的uitableviewcell。我正在使用的代码如下。当我使用UITableViewCell实例化alloc ...我得到表分隔符,但是当我使用thumbCell alloc时,表行分隔符行没有出现,我在thumbCell中设置的文本没有出现..并且屏幕出现全白..请帮助我理解我做错了什么..

在我的ViewController中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"GroupCell";
    thumbCell *cell = [self.testTableView dequeueReusableCellWithIdentifier:identifier];
    //UITableViewCell *cell = [self.testTableView dequeueReusableCellWithIdentifier:identifier]; (this works)

    if(!cell) {
        cell = [[thumbCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
       //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];(this works)
    }
    [cell.textLabel setText:@"test"];
    return cell;
}

和thumbCell子类

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self.textLabel setText:@"thumbCell"];
    }
    return self;
}

1 个答案:

答案 0 :(得分:0)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
 static NSString *identifier = @"GroupCell";
thumbCell *cell = [self.testTableView dequeueReusableCellWithIdentifier:identifier];
//UITableViewCell *cell = [self.testTableView dequeueReusableCellWithIdentifier:identifier]; (this works)

if(!cell) {
    cell = [[thumbCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
   //cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];(this works)
}
NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]];
NSInteger row = indexPath.row;
[cell setCellBackground:sectionRows:row];
[cell.textLabel setText:@"test"];

return cell;
}

在thumbCell类(自定义单元格类)

-(void)setCellBackground:(NSInteger)sectionRows:(NSInteger)row
{
UIImage *rowBackground;
UIImage *selectionBackground;

if (row == 0 && row == sectionRows - 1)
{
    rowBackground = [UIImage imageNamed:@"topAndBottomRow.png"];
    selectionBackground = [UIImage imageNamed:@"topAndBottomRowSelected.png"];
}
else if (row == 0)
{
    rowBackground = [UIImage imageNamed:@"topRow.png"];
    selectionBackground = [UIImage imageNamed:@"topRowSelected.png"];
}
else if (row == sectionRows - 1)
{
    rowBackground = [UIImage imageNamed:@"bottomRow.png"];
    selectionBackground = [UIImage imageNamed:@"bottomRowSelected.png"];
}
else
{
    rowBackground = [UIImage imageNamed:@"middleRow.png"];
    selectionBackground = [UIImage imageNamed:@"middleRowSelected.png"];
}
((UIImageView *)self.backgroundView).image = rowBackground;
((UIImageView *)self.selectedBackgroundView).image = selectionBackground;
}