How can i return the part of a string in a cell in Xcode?
I have a json responce:
{"Email":"john#appleseed.com","Password":"4321","lijst":"eieren, kaas, melk, 2 uien, brood(volkoren)"}
how can i get the value's at "lijst" in different cells in xcode?
So cell 1 = Eieren
cell 2 = Kaas
etc...
this is the code to display it all in one cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
switch (indexPath.row) {
case 0:
cell.textLabel.text = [self name];
cell.detailTextLabel.text = #"Name";
break;
case 1: {
NSString *email = [details objectForKey:#"Email"];
if (!email)
email = #"No email";
if ([email isKindOfClass:[NSArray class]])
email = #"<Multiple emails>";
cell.textLabel.text = email;
cell.detailTextLabel.text = #"Email";
break;
}
case 2:
cell.textLabel.text = self.details[#"lijst"];
cell.detailTextLabel.text = #"List";
break;
default:
break;
}
return cell;
}
It appears that you have parsed this JSON into a dictionary called self.details. You could then, as #bdesham pointed out, get an array of the individual values with componentsSeparatedByString:. For example, if you had a NSArray property called lijstArray, you could do something like:
NSString *lijstString = self.details[#"lijst"];
self.lijstArray = [lijstString componentsSeparatedByString:#", "];
Then, lijstArray[0] will have the first value, lijstArray[1] the second, etc. The number of values is [lijstArray count].
The above only works if you know that there will be exactly one and only one space after the commas. If you don't know that for sure, you'd just use #"," for the separator, but then trim the whitespace. For example, if you declared the lijstArray property to be a NSMutableArray, you could do something like:
NSString *lijstString = self.details[#"lijst"];
NSArray *tempArray = [lijstString componentsSeparatedByString:#","];
self.lijstArray = [NSMutableArray array];
for (NSString *lijstValue in tempArray) {
[self.lijstArray addObject:[lijstValue stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}
Whichever approach you adopt, having populated an array, lijstArray, you can now use that in your cellForRowAtIndexPath. For example, let's assume your tableview had two sections, section 0 being the name and email, and section 1 being the individual lijstArray values, e.g.:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return 2;
else if (section == 1)
return [self.lijstArray count];
return 0; // we never should get here, but in the spirit of defensive programming, let's make sure we handle this
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
cell.textLabel.text = [self name];
cell.detailTextLabel.text = #"Name";
break;
case 1: {
NSString *email = [details objectForKey:#"Email"];
if (!email)
email = #"No email";
if ([email isKindOfClass:[NSArray class]])
email = #"<Multiple emails>";
cell.textLabel.text = email;
cell.detailTextLabel.text = #"Email";
break;
}
default:
break;
}
} else if (indexPath.section == 1) {
cell.textLabel.text = self.lijstArray[indexPath.row];
}
return cell;
}
Related
I have implemented a viewController that contains a tableView which display user's data(firstName , lastName , birthday..) fetched from login Facebook or twitter or google Plus .I have made three custom cells : firstNameTableViewCell , lastNameTableViewCell,birthdayTableViewCell : All of this three cells contains a label ans a textfield.I want that when the user touch the corresponding cell , he will be able to change his data.
Technically , I have used the tableView's delegate methods : cellForRowAtIndexPath & didSelectRowAtIndexPath but I don't have what I want : When I open the interface : the label is disappering and when i touch the corresponding cell , nothing that changes .
This my code :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row)
{
case 0 : {
NSString *cellIdentifier = [[NSString alloc] initWithFormat:#"genderCell"];
genderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[self.tableUserData objectAtIndex:indexPath.row]];
cell.maleImageView = [cell showGender:self.gender];
cell.femaleImageView =[cell showGender:self.gender];
return cell ;
break;
}
case 1 : {
NSString *cellIdentifier = [[NSString alloc] initWithFormat:#"firstNameCell"];
firstNameTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[self.tableUserData objectAtIndex:indexPath.row]];
cell.firstNameLabel.text = [NSString stringWithFormat:#"%#",self.firstName];
cell.firstNameTextField.hidden =YES ;
return cell;
break;
}
case 2 : {
NSString *cellIdentifier = [[NSString alloc] initWithFormat:#"lastNameCell"];
lastNameTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[self.tableUserData objectAtIndex:indexPath.row]];
cell.lastNameLabel.text = [NSString stringWithFormat:#"%#",self.lastName];
cell.lastNameTextField.hidden = YES ;
return cell;
break;
}
case 3 : {
NSString *cellIdentifier = [[NSString alloc] initWithFormat:#"BirthDayCell"];
birthDayTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[self.tableUserData objectAtIndex:indexPath.row]];
cell.birthDaylabel.text = [NSString stringWithFormat:#"%#",self.birthDay];
cell.birthDayTextField.hidden = YES ;
return cell;
break;
}
case 4 : {
NSString *cellIdentifier = [[NSString alloc] initWithFormat:#"heightCell"];
heightTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[self.tableUserData objectAtIndex:indexPath.row]];
cell.heightUnityLabel.text = [NSString stringWithFormat:#"%#",self.heightUnity];
self.height = [cell.heightTextField.text floatValue];
return cell;
break;
}
case 5 : {
NSString *cellIdentifier = [[NSString alloc] initWithFormat:#"weightCell"];
weightTableViewCell *cell= [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[self.tableUserData objectAtIndex:indexPath.row]];
cell.weightUnityLabel.text = [NSString stringWithFormat:#"%#",self.weightUnity];
self.weight = [cell.weightTextField.text floatValue];
return cell;
break;
}
default:
break ;
}
return nil ;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"%ld",indexPath.row);
if (indexPath.row == 1) {
firstNameTableViewCell *cell = [firstNameTableViewCell new];
cell.firstNameLabel.hidden = YES ;
cell.firstNameTextField.hidden = NO ;
cell.firstNameTextField.text = cell.firstNameLabel.text ;
[self.userDataTableView reloadData];
}
else if (indexPath.row == 2) {
lastNameTableViewCell *cell = [lastNameTableViewCell new];
cell.lastNameLabel.hidden = YES ;
cell.lastNameTextField.hidden = NO ;
cell.lastNameTextField.text = cell.lastNameLabel.text ;
[self.userDataTableView reloadData];
}
else if (indexPath.row == 3) {
birthDayTableViewCell *cell = [birthDayTableViewCell new];
cell.birthDaylabel.hidden = YES ;
cell.birthDayTextField.hidden = NO ;
cell.birthDayTextField.text = cell.birthDaylabel.text ;
[self.userDataTableView reloadData];
}
}
Inside:
-(void)tableView:tableView didSelectRowAtIndexPath:indexPath
It is much better if you'll just use the indexPath to return the cell like:
firstNameTableViewCell *cell = (firstNameTableViewCell *)[tableView cellForRowAtIndexPath:(NSIndexPath)];
..
cell.firstNameTextField.hidden = NO ;
cell.firstNameTextField.text = cell.firstNameLabel.text;
// but do not reload the table yet..
Instead make cell.firstNameTextField as the first responder and let the user enter necessary data..
[cell.firstNameTextField becomeFirstResponder];
if the user is done editing and you have database for that, update your database on DONE, then your table dataSource (self.tableUserData), only then you need the [self.userDataTableView reloadData];, and i think you dont really want to reload the whole table for that single edit,[self.userDataTableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:(UITableViewRowAnimation)] reloading only the target indexPath is much more suitable for that..
-reloadData means 'reloading everything in the table based from your array data source'. meaning settings of values made inside:
-(void)tableView:tableView willDisplayingCell:cell forRowAtIndexPath:indexPath
and
-(UITableViewCell *)tableView:tableView cellForRowAtIndexPath:indexPath
ex:
cell.firstNameLabel.text = [NSString stringWithFormat:#"%#",self.firstName];
cell.lastNameTextField.hidden = YES ;
will be refreshed and set as is...
Edit
Also i've noticed that you didn't allocate your cells if nil after dequeueReusableCellWithIdentifier(i wonder why your app is not crashing because of it), anyway it is good for the memory to reuseCells like:
firstNameTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
cell = [[firstNameTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
We are using an uitableview and allow users to delete uitableviewcells, but a strange bug occurs, where two delete buttons appear.
Has anyone seen this before or have any ideas how to resolve this issue?
Thank you for your help and time.
Regards
Jing Jing Tao
UPDATE 1
Code for my cell at index for row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
//cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
//cell.detailTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.detailTextLabel.numberOfLines = 0;
}
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
UPDATE 2
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
//Sections logic
Model *info;
switch ([indexPath section])
{
case 0:
info = [self.bapiArray objectAtIndex:[indexPath row]];
break;
case 1:
info = [self.tablesArray objectAtIndex:[indexPath row]];
break;
case 2:
info = [self.tcodesArray objectAtIndex:[indexPath row]];
break;
default:
break;
}
cell.textLabel.text = info.subsection;
cell.detailTextLabel.text = info.text;
if(!(info.program == (id)[NSNull null] || info.program.length == 0))
{
cell.detailTextLabel.text = [NSString stringWithFormat:#"%# Program:%#", info.text,info.program];
}
}
Why the UITableViewCells don't reload the checkmarks after selecting, scrolling away, then scrolling back?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
#define CHECK_NULL_STRING(str) ([str isKindOfClass:[NSNull class]] || !str)?#"":str
static NSString *CellIdentifier = #"inviteCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.textLabel.highlightedTextColor = [UIColor colorWithHexString:#"#669900"];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.backgroundColor = [UIColor blackColor];
cell.textLabel.textColor = [UIColor whiteColor];
[[UITableViewCell appearance] setTintColor:[UIColor colorWithHexString:#"#669900"]];
if (cell == nil) {
cell = [[UITableViewCell alloc] init];
}
if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; }
BOOL isSearching = tableView != self.tableView;
NSArray *arrayToUse = (isSearching ? searchResults : contactsObjects);
id p = arrayToUse[indexPath.row];
NSString *fName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByFirstName));
NSString *lName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByLastName));
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", CHECK_NULL_STRING(fName), CHECK_NULL_STRING(lName)];
BOOL showCheckmark = [[stateArray objectAtIndex:indexPath.row] boolValue];
if (showCheckmark == YES)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSLog(#"It hit showCheckmark = YES, and stateArray is %#",stateArray[indexPath.row]);
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
NSLog(#"It hit showCheckmark = NO, and stateArray is %#",stateArray[indexPath.row]);
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
id object = contactsObjects[indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[stateArray insertObject:[NSNumber numberWithBool:YES] atIndex:indexPath.row];
[selectedObjects addObject:object];
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
[stateArray insertObject:[NSNumber numberWithBool:NO] atIndex:indexPath.row];
[selectedObjects removeObject:object];
}
//slow-motion selection animation.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
You missed out the ! (inverse operator) on the following line meaning that the state will always be the same.
[stateArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:[[stateArray objectAtIndex:indexPath.row] boolValue]]];
It should be
[stateArray replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:![[stateArray objectAtIndex:indexPath.row] boolValue]]];
Edit --- I've refactored both methods because it can be done with a lot less code and it will completely simplify the methods for you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"inviteCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
BOOL isSearching = tableView != self.tableView;
NSArray *arrayToUse = (isSearching ? searchResults : contactsObjects);
id p = arrayToUse[indexPath.row];
NSString *fName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByFirstName));
NSString *lName = (__bridge_transfer NSString *)(ABRecordCopyValue((__bridge ABRecordRef)(p), kABPersonSortByLastName));
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", CHECK_NULL_STRING(fName), CHECK_NULL_STRING(lName)];
BOOL showCheckmark = [stateArray[indexPath.row] boolValue];
if (showCheckmark == YES) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
id object = contactsObjects[indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[selectedObjects addObject:object];
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
[selectedObjects removeObject:object];
}
stateArray[indexPath.row] = #(cell.accessoryType == UITableViewCellAccessoryCheckmark);
}
I suggest a more object oriented approach. This will ensure that your code is flexible and displays correctly all the time.
For each item you wish to display in your table, have a corresponding object. You mentioned that you are displaying contacts, so let's suppose your object is called "Contact":
//Contact.h
#interface Contact : NSObject
#property BOOL selected;
#property NSString *name;
#end
//Contact.m
#import Contact.h
#implementation Contact
+ (id) contactWithName:(NSString*)name {
Contact *nContact = [Contact new];
nContact.name = name;
nContact.selected = NO;
return nContact;
}
#end
Then, just make your view work something like this:
//ContactView.m
#interface ContactView()
#property NSMutableArray *contacts;
#end
#implementation ContactView
#synthesize contacts;
- (void) viewDidLoad {
[super viewDidLoad];
//get your contact list here. When creating contacts, be sure to assign their selected and their name as you require.
contacts = #[[Contact contactWithName:#"John"], [Contact contactWithName:#"Jane"]];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = #"inviteCell";
UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
Contact *cellContact = [contacts objectAtIndex:indexPath.row];
cell.textLabel.text = cellContact.name;
cell.accessoryType = cellContact.selected == YES ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Contact *cellContact = [contacts objectAtIndex:indexPath.row];
cellContact.selected = !cellContact.selected;
[contacts replaceObjectAtIndex:indexPath.row withObject:cellContact];
[tableView reloadData]; //to refresh without animation
//[tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [tableView numberOfSections])] withRowAnimation:UITableViewRowAnimationTop]; //to refresh with animation
}
#end
And boom, easy to use tables that always look right, queue properly, and are object oriented for easy maintenance later.
The cell selection problem was not solved, even when insertObject was replaced with replaceWithObject, however, one should not waste time setting BOOL objects with an NSInteger inside an NSMutableArray. Instead, for cell selection memory, one should use NSDictionary like this:
#property (nonatomic, strong) NSMutableDictionary * selectedRowCollection;
- (void)viewDidLoad{
self.selectedRowCollection = [[NSMutableDictionary alloc] init];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
id object = contactsObjects[indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedRowCollection setObject:#"1" forKey:[NSString stringWithFormat:#"%d",indexPath.row]];
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedRowCollection removeObjectForKey:[NSString stringWithFormat:#"%d",indexPath.row]];
}
//slow-motion selection animation.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL showCheckmark = [[self.selectedRowCollection valueForKey:[NSString stringWithFormat:#"%d",indexPath.row]] boolValue];
if (showCheckmark == YES)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
You are not doing this UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
Allocating is necessary if cell is nil. Or it cause problem while scrolling.
I have 3 tableViews in one view and for some reason two of them are showing data but one isn't, what's my issue?!!
I'm guessing the issue is in the cellForRowAtIndexPath declaration. Attached it is.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView==_titleTV) {
NSDictionary *dictionaryTwo = [_titleArray objectAtIndex:indexPath.section];
NSArray *arrayTwo = [dictionaryTwo objectForKey:#"data"];
NSString *cellValueTwo = [arrayTwo objectAtIndex:indexPath.row];
cell.textLabel.text = cellValueTwo;
}
if (tableView==_cuisineTV) {
NSDictionary *dictionary = [_cuisineArray objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"data"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
}
else {
int storyIndex = [indexPath indexAtPosition: [indexPath length]-1];
cell.textLabel.text=[_favouritesArray objectAtIndex: storyIndex];
}
return cell;
}
What's the problem??
Thanks,
SebOH.
Try printing out the value of cellValueTwo to ensure its not a blank string:
NSLog(#"cellValueTwo value: %#",cellValueTwo);
Did you check to make sure the number of cells being created for _titleTV is correct with what you expect from - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section?
I'm using a tableview with sections and multiple selection, but I have an issue with multiple rows being checked when one row is chosen...
I've seen a few other threads about this, but didn't really get a solution...
Here's my code:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
{
[employeeTable deselectRowAtIndexPath:[employeeTable indexPathForSelectedRow] animated:NO];
UITableViewCell *cell = [employeeTable cellForRowAtIndexPath:indexPath];
// get the letter in each section
NSString *alphabet = [charIndex objectAtIndex:indexPath.section];
// get the names beginning with the letter
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF beginswith[c] %#", alphabet];
NSArray *names = [listOfNames filteredArrayUsingPredicate:predicate];
NSString *name = [names objectAtIndex:indexPath.row];
for(int i = 0; i < [employeeConnection.employees count]; i++)
{
Employee *aEmployee = [employeeConnection.employees objectAtIndex:i];
NSString *firstName = aEmployee.firstName;
NSString *lastName = aEmployee.lastName;
NSString *fullName = [NSString stringWithFormat:#"%# %#", firstName, lastName];
if([fullName isEqualToString:name])
{
NSLog(#"Name: %#", name);
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// Reflect selection in data model
[chosenEmployees addObject:aEmployee.employeeID];
[chosenEmployeesNames addObject:name];
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
// Reflect deselection in data model
[chosenEmployees removeObject:aEmployee.employeeID];
[chosenEmployeesNames removeObject:name];
}
}
}
}
Update: Added cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.textColor = [UIColor whiteColor];
}
// Get the letter in the current section
NSString *alphabet = [charIndex objectAtIndex:[indexPath section]];
// Get the names beginning with the letter
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF beginswith[c] %#", alphabet];
NSArray *names = [listOfNames filteredArrayUsingPredicate:predicate];
if([names count] > 0)
{
// Extract the name
cell.textLabel.text = [names objectAtIndex:indexPath.row];
}
return cell;
}
I would suggest storing an NSMutableSet of either the checked ManagedObject (when using CoreData) or simply the checked IndexPaths. In -cellForRowAtIndexPath: you can then check if the cell is supposed to be checked.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *const identifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
cell.textLabel.textColor = UIColor.whiteColor;
}
if ([self.checkedIndexPaths containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *const cell = [tableView cellForRowAtIndexPath:indexPath];
[table deselectRowAtIndexPath:indexPath animated:NO];
if ([self.checkedIndexPaths containsObject:indexPath]) {
[self.checkedIndexPaths removeObject:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else {
[self.checkedIndexPaths addObject:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
Since cells are being reused, you need to set the accessory mark to on or off for every cell in the table in the cellForRowAtInexPath table datasource method.
So the cell.accessoryType cell property should be soecified in the cellForRowAtInexPath and not the didSelectRow delegate method.
In the didSelectRow, just keep track of the selected rows in an array, and set the cells accessory mark to none or checkmark in the cellForRowAtInexPath dependingon the array value.