2

I'm setting up the way texLabel of a UITableViewCell is determined in cellForRowAtIndexPath, and the problem is that when pulling from agendaTableArray, it repeats the item in every section of the table, rather than only in the section it belongs to. In other words, I want each item in the array to be a row in its own section.

Here's how it currently looks:

enter image description here

Here's how I'm setting up cellForRowAtIndexPath:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"MyReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
        cell.textLabel.text = nil;
    }

    self.agendaTableArray = @[@"Dinner with Rebekah", @"Soccer game", @"Denist appt.", @"Celebrate job offer, drinks with Pierre!", @"No events today!"];

    // Set title of the event
    if (self.datePicked == [NSNumber numberWithInt:16]) {
        NSLog(@"cellForRowAtIndexPath says self.datePicked is 16");
    }
    else if (self.datePicked == [NSNumber numberWithInt:17]) {
        NSLog(@"cellForRowAtIndexPath says self.datePicked is 17");
    }
    else if (self.datePicked == [NSNumber numberWithInt:18]) {
        NSLog(@"cellForRowAtIndexPath says self.datePicked is 18");
    }
    else if (self.datePicked == [NSNumber numberWithInt:19]) {
        NSLog(@"cellForRowAtIndexPath says self.datePicked is 19");
    }
    else {

    }

    cell.textLabel.text = self.agendaTableArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:14];

    [self whatSectionsAreVisible];

    return cell;
}

How can I set it to only load the item from agendaTableArray in the first section's cell?

Andrew
  • 3,839
  • 10
  • 29
  • 42

4 Answers4

3

First, you should use this:

- dequeueReusableCellWithIdentifier:forIndexPath:

Instead of

- dequeueReusableCellWithIdentifier

Even if is not strictly necessary, here is a good explanation of why you should do it.

Second, you're assigning a new array every time a cell is requested to be drawn, and if for any reason you decide to change an element, it won't work, this line should be in your viewDidLoad to improve performance:

self.agendaTableArray = @[@"Dinner with Rebekah", @"Soccer game", @"Denist appt.", @"Celebrate job offer, drinks with Pierre!", @"No events today!"];

Third, you should never compare NSNumber values with a ==, two NSNumber will be exactly the same (==) only if they are really the same object, not the actual value of it, comparing two NSNumber you need to use isEqualToNumber:

// Assuming datePicked is a NSNumber
([self.datePicked isEqualsToNumber:[NSNumber numberWithInt:16]])

Finally to set a different element for each section you should do:

// Assuming there's only one element per section.
cell.textLabel.text = self.agendaTableArray[indexPath.section];
Community
  • 1
  • 1
Fantini
  • 2,067
  • 21
  • 32
0

Your are reusing the cells so, just after

if (!cell)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
}

set

cell.textLabel.text = nil;
Dante Puglisi
  • 648
  • 7
  • 24
  • And you are loading that for every cell, if you only want it in he first one, wrap it inside an if statement like this: if(indexPath.row == 0) { } – Dante Puglisi Jun 24 '15 at 17:17
0

You can check for which section cellForRowAtIndexPath is in by using :

if(indexPath.section==0){ 
//Logic for section 0
}else if(indePath.section==1){
//Logic for section 1
}

inside of that if for instance you want the first row of the first section this will do it:

if(indexPath.section==0 && indexPath.row==0){ 
   //Code here
}

I hope that helps. Of course instead of && you can nest indexPath.row==0 inside of the first if like so:

if(indexPath.section==0){ 
  if(indexPath.row==0){
    //Code here
  }
}
wilforeal
  • 369
  • 3
  • 13
0

First Set the agendaTableArray in a different place like init, viewDidLoad or viewWillAppear.

Also I'm guessing you have in numberOfSectionsInTableView: something like this

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return N; // the number of sections you want to display
}

And numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.agendaTableArray.count;
}

Then you may want to compare those numbers maybe like this

if ([self.datePicked intValue] == 16) {
    NSLog(@"cellForRowAtIndexPath says self.datePicked is 16");
}

in which case I recommend to use a switch condition

OR as suggested using isEqualsToNumber:

Finally take this line

cell.textLabel.text = nil;

out of the if condition

My suggestion

You may want to create your custom UITableViewCell and initialized it with a default cell style UITableViewCellStyleSubtitle, then override the cell's prepareForReuse to reset the content of the reusable cell.

Then in viewDidLoad you can register and you can dispose the if condition if (!cell){...} this way your code will look more modular and cleaner

Black Sheep
  • 1,665
  • 1
  • 22
  • 32