質問

i made a custom cell having a label and an imageView, the custom cell cell has a class customCellClass with an identifier CustomTableCell. this is the result screen shot, no data has been passed but the custom table appears as u can see

screen shot

this is the .m file where im getting the data from an Array *name. please see what am i missing. and btw im trying to [self.tableView reloadData]; in viewdidload but i dont know y, but i cant possible initiate that.

@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

btw this is the custom table cell im using

enter image description here

役に立ちましたか?

解決

You doing alloc init and then check it to nil... in you case you never pass this 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];
    }

Just do it:

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

他のヒント

You can also try this:

Create Class File like this:

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

And extend your custom cell by CustomRankingCell rather than UITableViewCell like this

#import "CustomRankingCell.h"

@interface RankingCell : CustomRankingCell
{

}

and then use custom cell like this:

static NSString *CellIdentifier = @"RankingCell";

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

Hope it Helps!!

This is what I used for my custom cell in a table and it works perfectly.

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

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

try this hope help for you..

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top