theomen
theomen

Reputation: 919

Table view doesn't show up custom cells

I have a view controller that shows up a table view controller when a button is clicked. Everything works fine with table view delegates, table view is shown fine, but in ellForRowAtIndexPath: delegate method, cell is instantiated and returned, but it's not shown correctly.

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

    static NSString *CellIdentifier = @"alrededor";

    alrededorCell *cell = [tableView 
                           dequeueReusableCellWithIdentifier:CellIdentifier];

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


    NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];

    NSLog(@"categoria %@", categoria);

    cell.title.text = [categoria valueForKey:@"title"];

    return cell;

}

Many thanks

Upvotes: 0

Views: 861

Answers (2)

TegRa
TegRa

Reputation: 519

If the cell you are loading is a subclass of UITableViewCell and you've used the interface builder to build the cell you have to do a couple of things.

In the nib file delete the view that is there when you create it, add a UITableViewCell and change it's class to alrededorCell. Changes the cells class and not the file's owner. When you are linking buttons,labels etc . be sure to link them to the cell not to file's owner. Also set the cells uniqueIdentifier to alrededor.

In cellForRowAtIndexPath

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

    static NSString *CellIdentifier = @"alrededor";

    alrededorCell *cell = [tableView 
                       dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *xib = [[NSBundle mainBundle] loadNibNamed:@"nibName" owner:nil options:nil];
        for (alrededorCell *view in xib) {
            if ([view isKindOfClass:[alrededorCell class]]) {
                cell = view;
            }
        }
    }


    NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];

    NSLog(@"categoria %@", categoria);

    cell.title.text = [categoria valueForKey:@"title"];

    return cell;

}

Upvotes: 1

Serdar Dogruyol
Serdar Dogruyol

Reputation: 5157

Why are you creating your cell like this?

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

If it is your own custom cell why do you use UITableViewCellStyleDefault ?

Upvotes: 2

Related Questions