我制作了一个带有标签和imageView的自定义单元格,自定义单元格单元格有一个带有标识符CustomTableCell的类customCellClass。 这是结果屏幕截图,没有数据传递但是自定义表显示为你可以看到
这是.m文件,其中我从Array *名称获取数据。请看我错过了什么。和顺便说一下我试图[self.tableView reloadData];在viewdidload但我不知道你,但我无法启动它。
@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
顺便说一下,这是使用
的自定义表格单元格
答案 0 :(得分:2)
您正在执行alloc init
,然后将其检查为nil ......如果您从未通过此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];
}
答案 1 :(得分: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"];
}
希望它能帮助!!
答案 2 :(得分:0)
这就是我在表格中使用自定义单元格时的效果。
static NSString *CellIdentifier = @"CustomTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
答案 3 :(得分: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;
}
试试这个希望帮助你..