2013-08-16 5 views
0

Я создал пользовательскую ячейку, имеющую метку и изображение. В ячейке пользовательской ячейки есть класс customCellClass с идентификатором CustomTableCell. это результат снимок экрана, не было принято никаких данных, но пользовательские таблицы появляется как вы можете видетьCustomCell: невозможно отобразить данные на главном экране

screen shot

это является .m файл, где им получать данные из массива * имя. посмотрите, что мне не хватает. и btw im пытается [self.tableView reloadData]; in 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 

Кстати это настраиваемая таблица клеток им с помощью

enter image description here

+1

ha Вы назначили делегата и источник данных для таблицы? –

+0

datacource и делегат ......? –

+0

Вы уверены, что ваши выходные соединения верны? –

ответ

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]; 
     } 
+0

im, имеющее это исключение Завершение приложения из-за неперехваченного исключения «NSInternalInconsistencyException», причина: «Невозможно загрузить NIB в пакете:« NSBundle » (загружен) 'с именем «CustomTableCell»' –

+0

Итак, прежде чем вы даже никогда не пытаетесь загрузить yuor cell! – Leta0n

+0

Проверить имя настраиваемой ячейки – Leta0n

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"]; 
} 

Надеюсь, поможет!!

+0

точно такой же результат. спасибо за ответ тоже. –

+0

попробуйте отредактировать код. он будет работать уверенно .. –

0

Это то, что я использовал для своей пользовательской ячейки в таблице, и она отлично работает.

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

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 
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; 
} 

попробовать это надеюсь, помощь вам ..

+0

Почему просто код ... Без объяснений? – Leta0n

+0

здесь mucustomecell реализуется customecell, и вы можете получить данные в метке «имя» вместо текстового поля «txtData» .. –

+0

это код, который я только что реализовал. только [cell setSelectionStyle: UITableViewCellSelectionStyleNone]; это тот, который отсутствует, но попробуем это тоже –

Смежные вопросы