following code starts with a blank cells and add a checkmark when selected cell but, once added, if you press again that cell, it is not erased. I am trying to reset it each time that didSelectRowAtIndexPath is called. How to clear cell in order to be marked only one? Thanks.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
labelInfo.text=#"1";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 1:
labelInfo.text=#"2";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 2:
labelInfo.text=#"3";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 3:
labelInfo.text=#"4";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 4:
labelInfo.text=#"5";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
default:
break;
}
}
}
This code will remove all checkmarks on the table
[[self.tableView visibleCells] makeObjectsPerformSelector:#selector(setAccessoryType:) withObject:UITableViewCellAccessoryNone];
Just copy-paste this. It works.
//in your .h file
int selectedCell;
//in your .m file
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* cellIdentifier = #"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = (UITableViewCell*) [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryNone;
if (indexPath.section == 0) {
if(indexPath.row == selectedCell)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.selected = YES;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selected = NO;
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedCell = indexPath.row;
[tableView reloadData];
//remaining code.
}
Try this:
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
In your code you start with
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
Which remove check mark from selected cell, but later you assign checkmark again with
switch (indexPath.row) {
case 0:
labelInfo.text=#"1";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 1:
labelInfo.text=#"2";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 2:
labelInfo.text=#"3";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 3:
labelInfo.text=#"4";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 4:
labelInfo.text=#"5";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
default:
break;
}
So, you can use something like
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
for toggle the checkmark and
labelInfo.text = [NSString stringWithFormat:#"%d", indexPath.row + 1];
for label
do this:-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
labelInfo.text=#"1";
if (cell.accessoryType==UITableViewCellAccessoryCheckmark)
cell.accessoryType=UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 1:
labelInfo.text=#"2";
if (cell.accessoryType==UITableViewCellAccessoryCheckmark)
cell.accessoryType=UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 2:
labelInfo.text=#"3";
if (cell.accessoryType==UITableViewCellAccessoryCheckmark)
cell.accessoryType=UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 3:
labelInfo.text=#"4";
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
case 4:
labelInfo.text=#"5";
if (cell.accessoryType==UITableViewCellAccessoryCheckmark)
cell.accessoryType=UITableViewCellAccessoryNone;
else
cell.accessoryType = UITableViewCellAccessoryCheckmark;
break;
default:
break;
}
}
Related
Spent hours and hours and no use.I don't understand whether I was not effective in searching(googling) or is it that there are less questions on this or I might have committed some mistake while implementing the answers of experts!
I know there are several questions on setting accessory type check mark for one row and none for other rows in a section,traced out posts here and there.
I have 2 sections in my table view.By default I want the 1st row in each section to be selected i.e. with accessory view check mark.Now from here upon user selection of a row,I want the check mark to be visible on selected row only.I have tried declaring two index paths to keep track of row selection in each section.Here is my implementation code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.row,indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (indexPath.section == 0)
{
if(self.firstSectionIndex == indexPath)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
if (indexPath.section == 1)
{
if(self.secondSectionIndex == indexPath)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
switch (indexPath.section)
{
case 0:
{
if (indexPath.row == 0)
{
if(self.firstSectionIndex != indexPath)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = #"Yes";
}
if (indexPath.row == 1)
{
cell.textLabel.text = #"No";
}
}
break;
case 1:
{
if (indexPath.row == 0)
{
if(self.secondSectionIndex != indexPath)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.text = #"Easy";
}
if (indexPath.row == 1)
{
cell.textLabel.text = #"Medium";
}
if (indexPath.row == 2)
{
cell.textLabel.text = #"Hard";
}
}
break;
default:
break;
}
}
tableView.backgroundColor = [UIColor clearColor];
tableView.backgroundView = nil;
return cell;
}
in my didSelectRowAtIndexPath,I have done the following:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
self.firstSectionIndex = indexPath;
}
if (indexPath.section == 1)
{
self.secondSectionIndex = indexPath;
}
[tableView reloadData];
}
I have searched for the state of cell selection to be saved for long term reference,in quest of it,I found some useful link here.
But it is selecting multiple cells and accessory type check mark is being applied for all rows in section.I don't understand what's wrong.Can any one please guide me on this!!
Thanks all in advance :)
you can achieve it using this bellow implementation of code :-
EDITED
.h file
NSMutableArray *firstSelectedCellsArray;
NSMutableArray *secondSelectedCellsArray;
NSMutableArray *ThirdSelectedCellsArray;
in .m file
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
{
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
if ( [firstSelectedCellsArray containsObject:rowNumber] )
{
[firstSelectedCellsArray removeObject:rowNumber];
}
else
{
[firstSelectedCellsArray addObject:rowNumber];
}
}
else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
if ( [firstSelectedCellsArray containsObject:rowNumber] )
{
[firstSelectedCellsArray removeObject:rowNumber];
}
else
{
[firstSelectedCellsArray addObject:rowNumber];
}
selectedCell.accessoryType = UITableViewCellAccessoryNone;
}
}
if (indexPath.section == 1)
{
NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
{
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
if ( [secondSelectedCellsArray containsObject:rowNumber] )
{
[secondSelectedCellsArray removeObject:rowNumber];
}
else
{
[secondSelectedCellsArray addObject:rowNumber];
}
}
else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
if ( [secondSelectedCellsArray containsObject:rowNumber] )
{
[secondSelectedCellsArray removeObject:rowNumber];
}
else
{
[secondSelectedCellsArray addObject:rowNumber];
}
selectedCell.accessoryType = UITableViewCellAccessoryNone;
}
}
if (indexPath.section == 2)
{
NSNumber *rowNumber = [NSNumber numberWithUnsignedInt:indexPath.row];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryNone)
{
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
if ( [ThirdSelectedCellsArray containsObject:rowNumber] )
{
[ThirdSelectedCellsArray removeObject:rowNumber];
}
else
{
[ThirdSelectedCellsArray addObject:rowNumber];
}
}
else if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
if ( [ThirdSelectedCellsArray containsObject:rowNumber] )
{
[ThirdSelectedCellsArray removeObject:rowNumber];
}
else
{
[ThirdSelectedCellsArray addObject:rowNumber];
}
selectedCell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
NOW in cellForRowAtIndexPath put littel piece of code:-
- (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] autorelease];
}
if(indexPath.section==0)
{
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [firstSelectedCellsArray containsObject:rowNsNum] )
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if(indexPath.section==1)
{
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [secondSelectedCellsArray containsObject:rowNsNum] )
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
if(indexPath.section==2)
{
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [ThirdSelectedCellsArray containsObject:rowNsNum] )
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
return cell;
}
DEMO LINK
http://www.sendspace.com/file/z6oxg2
ScreenShot:
I have found a precise and perfect solution,thanks to Lithu T.V and Nithin Gobel for guiding me in the right direction.Unfortunately their solutions didn't help me in achieving 1 check mark per section,but multiple check marks in fact.So I thought of saving the selected row in user defaults and for initially selecting 1st row of each section,we declare first and second section index,assign to index path and then assign cell accessory view as check mark.Here we go,let's first deal with cellForRowAtIndexPath:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:#"S%1dR%1d",indexPath.row,indexPath.section];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
NSUserDefaults *savedState = [NSUserDefaults standardUserDefaults];
switch (indexPath.section)
{
case 0:
{
NSNumber *indexNumber = [NSNumber numberWithInteger:indexPath.row];
if([[savedState objectForKey:#"firstSectionIndex"] isEqual: indexNumber])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.firstSectionIndex = indexPath;
}
if (indexPath.row == 0)
{
NSObject * checkedCellObject = [savedState objectForKey:#"firstSectionIndex"];
if(checkedCellObject == nil)
{
self.firstSectionIndex = indexPath;
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
cell.textLabel.text = #"Yes";
}
if (indexPath.row == 1)
{
cell.textLabel.text = #"No";
}
}
break;
case 1:
{
NSNumber *indexNumber = [NSNumber numberWithInteger:indexPath.row];
if([[savedState objectForKey:#"secondSectionIndex"] isEqual: indexNumber])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
secondSectionIndex = indexPath;
}
if (indexPath.row == 0)
{
NSObject * checkedCellObject = [savedState objectForKey:#"secondSectionIndex"];
if(checkedCellObject == nil)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
secondSectionIndex = indexPath;
}
cell.textLabel.text = #"Easy";
}
if (indexPath.row == 1)
{
cell.textLabel.text = #"Medium";
}
if (indexPath.row == 2)
{
cell.textLabel.text = #"Hard";
}
}
break;
default:
break;
}
}
tableView.backgroundColor = [UIColor clearColor];
tableView.backgroundView = nil;
return cell;
}
Please observe in each case,we are checking if the checked state is in saved user defaults,if it is nil,we check 1st cell(row) of each section,here we go with table view delegate method,did select row at index path:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (indexPath.section == 0)
{
UITableViewCell *checkCell = [tableView cellForRowAtIndexPath:indexPath];
if(firstSectionIndex && firstSectionIndex != indexPath)
{
UITableViewCell *uncheckCell = [tableView cellForRowAtIndexPath:firstSectionIndex];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
self.firstSectionIndex = indexPath;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:firstSectionIndex.row] forKey:#"firstSectionIndex"];
checkCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
if (indexPath.section == 1)
{
UITableViewCell *checkCell = [tableView cellForRowAtIndexPath:indexPath];
if(secondSectionIndex)
{
UITableViewCell *unchekCell = [tableView cellForRowAtIndexPath:secondSectionIndex];
unchekCell.accessoryType = UITableViewCellAccessoryNone;
}
self.secondSectionIndex = indexPath;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:secondSectionIndex.row] forKey:#"secondSectionIndex"];
checkCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
That's it,we have successfully accomplished one check mark per section and also save the accessory state of cell in user defaults for long term reference.
Hope this helps some one,thanks :)
for implementing checkmark
Keep an index array for selected indexes
In the cellFor Row method check if current indexpath is there in the
selected index array.
if true
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
In didSelect method if checkmarked.Remove index from array as well
as set the accessory type none
If accessory none add the checkmark and add the index into array .
EDIT
In case what you need is the single selection everytime you select a cell remove all index from array and add that only and then reload tableview
I have the similar requirement where I need to select a single option per section and show the tick mark and I have achieved the same with simple steps. You do not need to reload the section or table at all.
Step 1:
Keep a hash-table/dictionary to your model which keeps the record of selected IndexPath per section type.
Example -
class DeviceSettingModel {
var indexPathSelectionDict: [DeviceSettingSectionType: IndexPath] = [:]
}
enum DeviceSettingSectionType: Int {
case audioDevices
case videoDevices
case videoQualities
}
Step 2:
Update the selection in tableView(didSelectAtRow) delegate:
Example -
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let settingModel = settingModel,
let type: DeviceSettingSectionType = DeviceSettingSectionType(rawValue: indexPath.section)
else { return }
// other data and view related updating...
// unselect first on the same section.
if let previousIndex = settingModel.indexPathSelectionDict[type] {
if let cell = tableView.cellForRow(at: previousIndex) {
cell.accessoryType = .none
}
}
// update selected indexPath into the model and put a tick mark
settingModel.indexPathSelectionDict[type] = indexPath
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = .checkmark
}
}
That's it, you can see below effect.
I want to implement Checklist for a single selection in a UITableView. Also, I need a cell to be selected by default. Here's my implementation in cellForRowAtIndexPath:
NSUInteger row = [indexPath row];
NSUInteger oldRow = [lastIndexPath row];
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
if (indexPath.row == selectedRow ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
didSelectRowAtIndexPath has this code:
if (!self.lastIndexPath) {
self.lastIndexPath = indexPath;
}
if ([self.lastIndexPath row] != [indexPath row])
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
self.lastIndexPath = indexPath;
}
else {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
With this code I'm able to get a default checkmark, but whenever I select another row
the first one remains selected until I don't click that cell. So, If I want to select my desired result what should I do?
`
I think the code is a little too complicated. All you need is a single property:
NSInteger _selectedRow;
This, when initially defined, will provide a default selected row. And it will also maintain the previous selection (when looking for the cell to 'unselect'):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_IDENTIFIER];
}
if ([indexPath row] == _selectedRow) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
cell.textLabel.text = [NSString stringWithFormat:#"Row %d", indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (_selectedRow >= 0) {
[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_selectedRow inSection:0]].accessoryType = UITableViewCellAccessoryNone;
}
_selectedRow = [indexPath row];
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
When this view is created, if you assign:
_selectedRow = 1;
Then the second row will automatically be selected. A value of -1 indicates no default selection, and the two above methods will automatically add/remove the checkmarks from tapped rows.
following code sets cell texts and add a checkMark only to last selected. Always only one cell check marked and works properly excepting when it is displayed for first time. So, text is not showed for that cell (only that one) until you press any other cell. For example, if cellPos = 4 when viewDidLoad, that cell will not contain text.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* cellIdentifier = #"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = (UITableViewCell*) [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryNone;
if (indexPath.section == 0) {
if(indexPath.row == cellPos)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.selected = YES;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selected = NO;
}
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"English";
cell.imageView.image = [UIImage imageNamed:#"english.png"];
break;
case 1:
cell.textLabel.text = #"Español";
cell.imageView.image = [UIImage imageNamed:#"spanish.png"];
break;
case 2:
cell.textLabel.text = #"Deutsch";
cell.imageView.image = [UIImage imageNamed:#"germany.png"];
break;
case 3:
cell.textLabel.text = #"Français";
cell.imageView.image = [UIImage imageNamed:#"french.png"];
break;
case 4:
cell.textLabel.text = #"Italiano";
cell.imageView.image = [UIImage imageNamed:#"italian.png"];
break;
default:
break;}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
cellPos = indexPath.row;
[tableView reloadData];
}
Have you tried moving the if-else block after the switch block? This way you set the text of the cell before you set the cell to selected. The fact that it only occurs the first time suggests to me that it may be an order of operations problem.
following code works properly but it applies same cell text to all sections. How could I apply selection case also depending on section? Thank you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"English";
break;
case 1:
cell.textLabel.text = #"Chinese";
break;
case 2:
cell.textLabel.text = #"Japanese";
break;
default:
break;
}
return cell;
}
You can use indexPath.section to get the section number, just like how you're using indexPath.row for the row number.
You have to check for the indexPath.section and also indexPath.row:
if (indexPath.section == 0) {
if (indexPath.row) {
...
}
} else if (indexPath.section == 1)
if (indexPath.row) {
...
}
} else if ...
I am writing a UITableView that contains 2 sections. When the table first loads all the cells are displaying the correct info, but when I start scrolling up and down, cells detailedTextLabel and accessoryType are being refreshed incorrectly, such that some cells that should only contain a detailedTextLabel also contain an accessory, and cells that should only contain an accessory also contain a detailedTextLabel.
Inside cellForRowAtIndexPath: I am using nested switch/case statements to apply the correct values to the cells in their respective section/row. As far as i can tell the logic in these statements is correct, so is it possible that the value of cell variable is inccorect when updating?
The table loads correctly but after scrolling accessoryType and detailedTextLabel gets mixed up.
Click for link to screen shots of the table.
Here is the code inside of my UITableViewController subclass:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [sectionNames count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
NSArray *headingsSection = [cellTitles objectAtIndex:section];
return [headingsSection count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sectionNames objectAtIndex:section];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
self.tableView.allowsSelection = YES;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [[cellTitles objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
switch (indexPath.section) {
case 0:
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d%%", [[assistSettingsArray_glob objectAtIndex:indexPath.row] intValue]];
break;
case 1:
switch (indexPath.row) {
case 0:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 1:
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 2:
if (defaultAssistOn) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
break;
}
break;
}
return cell;
}
Due to cell re-use, cells with previously set values re-appear.
Before the switch (indexPath.section) ..., initialize the detailTextLabel and accessoryType to defaults:
cell.detailTextLabel.text = #"";
cell.accessoryType = UITableViewCellAccessoryNone;
Try to enclose the code in each switch cases inside parantheisis. So the above code looks like this:
switch (indexPath.section) {
case 0: {
cell.detailTextLabel.text = [NSString stringWithFormat:#"%d%%",
[[assistSettingsArray_glob objectAtIndex:indexPath.row] intValue]];
break;
}
case 1: {
switch (indexPath.row) {
case 0: {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break; }
case 1: {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break; }
case 2: {
if (defaultAssistOn) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
break; }
default:
break;
}
break;
}
default:
break;
}
I am not sure whether this work. Do inform if it does :)