1

I've researched the site and other sources online, but couldn't find a direct addressing to what I am asking. I have the following code in my class:

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

     KJCustomAdTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"CustomerAdCell" bundle:nil] forCellReuseIdentifier:@"myCustomCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"myCustomCell"];
    }

    //cell.textLabel.text = [_adsObjectArray objectAtIndex:indexPath.row];
    //    AdObject *someAd = _adsObjectArray[0];
    //    cell.titlePl  aceholderLabel.text = someAd.title;
    //    cell.locationPlaceholderLabel.text = someAd.location;
    return cell;
}

When I tried this with an array of strings, it worked well and I could get a table with all items in the array. But when I tried with my array of AdObjects, it didn't work. I tried a for loop to go through the array, but learned that this method is called for creation of each cell row, hence caused my whole table to reflect the same title for all rows.

I seek your help or a tutorial/question in another location that would address this problem...

Thanks in advance!

7
  • Why do you have _adsObjectArray[0] instead of _adsObjectArray[indexPath.row]? Commented Apr 4, 2014 at 15:11
  • Explain how is _adsObjectArray is created. And what your want. The relevant code (pure guess from what I read from your code): AdObject *someAd = [_adsObjectArray objectAtIndex:[indexPath row]]; cellTitlePlaceholderLabel.text = someAd.title; Commented Apr 4, 2014 at 15:13
  • 1
    @dasblinkenlight and with your question you answered mine! Thanks mate Commented Apr 4, 2014 at 15:20
  • @dasblinkenlight if you want to create an answer and post it, I will select it. Your hint solved my problem Commented Apr 4, 2014 at 15:21
  • 1
    Just a comment on style. Move the registerNib into your viewDidLoad and you should only need to,call dequeue one time. In this case you're using different reuseIdentifiers as well. Seems like your code hasn't been fully transitioned yet. Commented Apr 4, 2014 at 15:22

2 Answers 2

2

To get a title from your array you need to do this:

AdObject *someAd = _adsObjectArray[indexPath.row];
cell.textLabel.text = someAd.title;

instead of:

AdObject *someAd = _adsObjectArray[0];
cell.textLabel.text = someAd.title;
Sign up to request clarification or add additional context in comments.

Comments

2

You need use object for current cell

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

     KJCustomAdTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (!cell) {
        [tableView registerNib:[UINib nibWithNibName:@"CustomerAdCell" bundle:nil] forCellReuseIdentifier:@"myCustomCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"myCustomCell"];
    }

    AdObject *someAd = [_adsObjectArray objectAtIndex:indexPath.row];
    cell.titlePlaceholderLabel.text = someAd.title;
    cell.locationPlaceholderLabel.text = someAd.location;
    return cell;
}

Also you are using "cell reusing" in wrong way and at least you should use same identifier for same cell type.

1 Comment

Thanks will fix that :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.