0

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

在我的视图控制器中

- (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;
}
4

1 回答 1

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;
}
于 2012-05-23T03:52:21.153 回答