Hey all,
I'm implementing an app from a book which displays a list of cities. Using a table view and nav controller. There's an Edit button on the right of the nav controller which when clicked allows the user to either delete a current city or add a new one.
here's the numberOfRowsInSection and cellForRowAtIndexPath, where cities is a NSMutableArray containing the cities.
Code:
- (NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section
{
// number of rows = number of City objects in citiesArray
NSInteger count = cities.count;
if(self.editing) //if edit button was pressed increase count so the 'Add City' row can be added
{
count = count + 1;
}
return count;
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"cell"];
if( nil == cell )
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cell"] autorelease];
}
if (indexPath.row < cities.count)
{
City *thisCity = [cities objectAtIndex:indexPath.row];
cell.textLabel.text = thisCity.cityName;
}
else
{
cell.textLabel.text = @"Add New City...";
cell.textLabel.textColor = [UIColor lightGrayColor];
cell.editingAccessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
In the cellForRowAtIndexPath method, how does the method know to only add the "Add City " option when in edit mode. My understanding of this method that the "add new City" section is only added when theindexPath.row == cities.count. But surely this is true regardless of whether the app os in Edit mode or not??
Thanks
mc