在一个UITableView问题中调用两个不同的自定义单元格 [英] Calling two different custom cell in one UITableView issue

查看:118
本文介绍了在一个UITableView问题中调用两个不同的自定义单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义单元格FeatureCell,它在行中有5个图像将在主视图中调用,但是当我调用它时,我得到空行。那么请问哪里可能是我的问题?

I have created a custom cell FeatureCell which has 5 images in the row that will be called in the main view but when I call it I get empty row. So please where could be my problem?

我已经搜索了自定义单元格,并且我使用了以下代码中使用的方式,但没有任何反应。

I have googled about custom cell and I used the way that I have to use in the code below but nothing happen.

这是我的FeatureCell.h

This is my FeatureCell.h

@interface FeatureCell : UITableViewCell{

  IBOutlet UIImageView *img1;
  IBOutlet UIImageView *img2;
}

@property (nonatomic, retain) UIImageView *img1;
@property (nonatomic, retain) UIImageView *img2;
@end

这是我的FeatureCell.m

This is my FeatureCell.m

@implementation FeatureCell

@synthesize img,img1,img2,img3,img4;
@end

这是我的视图控制器.m

This is the my view-controller .m

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{

return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

if (section == 0) {
    return [objectCollection count];
}
else{
    return 1;
   }
}    

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString* cellIdentifier1 = @"FeatureCell";

FeatureCell *cell1 = (FeatureCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];


if (cell1 == nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier1 owner:nil options:nil];

    cell1 = (FeatureCell*)[nib objectAtIndex:0];
}

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                  reuseIdentifier:CellIdentifier];

}


if ([indexPath section] == 0) {

    containerObject = [objectCollection objectAtIndex:indexPath.row];

    [[cell textLabel] setText:containerObject.store];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

}
    else{

    cell1.img1.image = [UIImage imageNamed:@"shower.png"];
    cell1.img2.image = [UIImage imageNamed:@"parking.png"];

    }
        return cell;
}


推荐答案

你需要做类似的事情这个:

You need to do something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                              reuseIdentifier:CellIdentifier];
        }

        containerObject = [objectCollection objectAtIndex:indexPath.row];
        [[cell textLabel] setText:containerObject.store];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    } else {
        static NSString* cellIdentifier1 = @"FeatureCell";

        FeatureCell *cell1 = (FeatureCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
        if (cell1 == nil) {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier1 owner:nil options:nil];
            cell1 = (FeatureCell*)[nib objectAtIndex:0];
        }

        cell1.img1.image = [UIImage imageNamed:@"shower.png"];
        cell1.img2.image = [UIImage imageNamed:@"parking.png"];

        return cell1;
    }
}

我可能有如果向后调整条件,仔细检查。

I might have the if condition backwards so double check that.

这篇关于在一个UITableView问题中调用两个不同的自定义单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆