0

我制作了一个具有标签和 imageView 的自定义单元格,自定义单元格单元格有一个带有标识符 CustomTableCell 的类 customCellClass。这是结果屏幕截图,没有传递任何数据,但自定义表格出现如您所见

截屏

这是我从数组 *name 获取数据的 .m 文件。请看看我错过了什么。顺便说一句,我正在尝试 [self.tableView reloadData];在 viewdidload 但我不知道 y,但我无法启动它。

@synthesize name = _name;
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.name = [NSArray arrayWithObjects:@"First", @"Second", @"Third",@"First", @"Second", @"Third",@"First", @"Second", @"Third",@"First", @"Second", @"Third",@"First", @"Second", @"Third", nil];
//    self.name = [[NSArray alloc]initWithObjects:@"First", @"Second", @"Third", nil];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.name count];

}

-(void) viewDidAppear:(BOOL)animated{

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"CustomTableCell";


    customCellClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[customCellClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.nameLabel.text = [self.name objectAtIndex:indexPath.row];
    cell.imageThumb.image = [UIImage imageNamed:@"images.jpeg"];
    return  cell;


}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 78;
}


@end

顺便说一句,这是我使用的自定义表格单元格

在此处输入图像描述

4

4 回答 4

2

你做alloc init然后检查它为零......在你的情况下你永远不会通过这个if

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

        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

去做就对了:

customCellClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[customCellClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CustomTableCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
于 2013-08-16T12:07:35.453 回答
1

你也可以试试这个:

像这样创建类文件:

CustomRankingCell.h

#import <UIKit/UIKit.h>

@interface CustomRankingCell : UITableViewCell

+(UITableViewCell *) cellFromNibNamed:(NSString *)nibName;

@end

CustomRankingCell.m

#import "CustomRankingCell.h"

@implementation CustomRankingCell



+ (CustomRankingCell *)cellFromNibNamed:(NSString *)nibName {

    NSArray *nibContents = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:NULL];
    NSEnumerator *nibEnumerator = [nibContents objectEnumerator];
    CustomRankingCell *xibBasedCell = nil;
    NSObject* nibItem = nil;

    while ((nibItem = [nibEnumerator nextObject]) != nil) {
        if ([nibItem isKindOfClass:[CustomRankingCell class]]) {
            xibBasedCell = (CustomRankingCell *)nibItem;
            break; // we have a winner
        }
    }

    return xibBasedCell;
}


@end

并通过 CustomRankingCell 而不是 UITableViewCell 这样扩展您的自定义单元格

#import "CustomRankingCell.h"

@interface RankingCell : CustomRankingCell
{

}

然后像这样使用自定义单元格:

static NSString *CellIdentifier = @"RankingCell";

RankingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = (RankingCell *)[RankingCell cellFromNibNamed:@"RankingCell"];
}

希望能帮助到你!!

于 2013-08-16T11:58:29.403 回答
0

这是我用于表格中的自定义单元格的内容,并且效果很好。

static NSString *CellIdentifier = @"CustomTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
于 2013-08-16T12:07:16.100 回答
0
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.name count];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    mycustomcell *cell = (mycustomcell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"mycustomcell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        NSLog( @"NIB = %@",nib);
        NSLog( @"Cell = %@",cell);
    }

    cell.txtData.text=[NSString stringWithFormat:@"%@",[self  objectAtIndex:indexPath.row]];

            cell.imgThumb.image = [UIImage imageNamed:@"images.jpeg"];
            cell.txtData.enabled=NO;


    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

试试这个希望对你有帮助..

于 2013-08-16T12:09:25.123 回答