I have searched for hours and hours and found many hints but not the solution. I have a very simple tableview with static cells. I am following the Apple documentation. I am not using storyboard, I have unchecked autolayout, set document versioning to iOS5. It works just fine in iOS6, but cells are NIL in iOS5 hence it will crash.
The cells are done in IB, I have the properties wired up, I have cell identifiers (actually not needed but does not make a difference). The log statement returns (null) in iOS5 but returns correctly a cell in iOS6.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0 ) {
if (indexPath.row ==0) {
cell0.selectionStyle = UITableViewCellSelectionStyleNone;
NSLog(#"description = %#",[cell0 description]);
return cell0;
}
if (indexPath.row ==1) {
cell1.selectionStyle = UITableViewCellSelectionStyleNone;
return cell1;
}
if (indexPath.row ==2) {
cell2.selectionStyle = UITableViewCellSelectionStyleNone;
return cell2;
}
if (indexPath.row ==3) {
cell3.selectionStyle = UITableViewCellSelectionStyleNone;
return cell3;
}
}
cell4.selectionStyle = UITableViewCellSelectionStyleNone;
return cell4;
}
Thanks in advance for pointing me in the right direction. I know it must be simple but it drives me crazy.
Replace your code with following code snippet it will work in ios5.
if (indexPath.section == 0 ) {
if (indexPath.row ==0) {
UITableViewCell *cell0 = [tableview dequeueReusableCellWithIdentifier:#"cell0"];
cell0.selectionStyle = UITableViewCellSelectionStyleNone;
NSLog(#"description = %#",[cell0 description]);
return cell0;
if (indexPath.row ==1) {
UITableViewCell *cell1 = [tableview dequeueReusableCellWithIdentifier:#"cell1"];
cell1.selectionStyle = UITableViewCellSelectionStyleNone;
return cell1;
}
if (indexPath.row ==2) {
UITableViewCell *cell2 = [tableview dequeueReusableCellWithIdentifier:#"cell2"];
cell2.selectionStyle = UITableViewCellSelectionStyleNone;
return cell2;
}
if (indexPath.row ==3) {
UITableViewCell *cell3 = [tableview dequeueReusableCellWithIdentifier:#"cell3"];
cell3.selectionStyle = UITableViewCellSelectionStyleNone;
return cell3;
}
UITableViewCell *cell4 = [tableview dequeueReusableCellWithIdentifier:#"cell4"];
cell4.selectionStyle = UITableViewCellSelectionStyleNone;
return cell4;
}
when you used dequeueReusableCellWithIdentifier: it can return nil if there are no cells created. You need to alloc and init your own cell at this point.
Related
I add a "Load More" cell in the tableview through check if the current cell is the last one.
but i have got 2 kinds of data to display,so i used a UISegmentedControl to switch.
once at the first tab of UISegmentedControl,the "Load More" cell appears at the last cell.but when i switch to second tab of the UISegmentedControl.The "Load More" cell still stays there...and there is still a "Load More" cell at the last of tableview.so there are 2 "Load More" cell shows.
i want to remove the first "Load More" cell,but i dont know how.Much THX.
EDIT:
static NSString * ID = #"TopicCustomCellIdentifier";
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if(IS_LAST_TOPIC_ROW){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
cell.textLabel.text = #"Load More";
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.imageView.image = nil;
return cell;
}
MTopicItem *topic;
cell.textLabel.textAlignment = NSTextAlignmentLeft;
if(SEGMENTINDEX == 0){
topic = [_my_topics_data_array objectAtIndex:indexPath.row];
}else{
topic = [_rec_topics_data_array objectAtIndex:indexPath.row];
}
[cell.textLabel setText:topic.name];
[cell.textLabel setFont:[UIFont systemFontOfSize:18]];
if(IS_RETINA){
[cell.imageView setImageWithURL:[NSURL URLWithString: topic.image]
placeholderImage:[UIImage imageNamed:#"default50"]];
}else{
[cell.imageView setImageWithURL:[NSURL URLWithString: topic.sImage]
placeholderImage:[UIImage imageNamed:#"default"]];
}
topic = nil;
return cell;
#define IS_LAST_TOPIC_ROW ((indexPath.row == [_my_topics_data_array count] - 1) && [_my_topics_data_array count] != 0) || ((indexPath.row == [_rec_topics_data_array count] - 1) && [_rec_topics_data_array count] != 0)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(SEGMENTINDEX == 0){
return [_my_topics_data_array count];
}else{
return [_rec_topics_data_array count];
}
}
Use the reloadData to refresh the view on segment touch.
[myTableView reloadData];
If you add a row manually, you can delete that manually too:
deleteRowsAtIndexPaths:withRowAnimation:
See here:
Check the last cell text label is load more don't add in second control.
This is happening due to the reusing of cell. When you deque a cell from the tableview check if it is not nil and reset content.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if(cell) {
cell.textLabel.text=#"";
cell.imageView.image=nil;
}
Also cross check if IS_LAST_TOPIC_ROW is identifying the last row correctly.
Hope that helps.
keep a boolean to make sure when to show the cell or not.
Check it before you add the loadmore cell in cellForRowAtIndexpath
method .
Also the numberOfRowsInSection should be maintained with the same
bool.
When you click the loadmore cell set boolean to no and then reload table
How it looks
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(showLoadMore)
{
if(IS_LAST_TOPIC_ROW){
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
cell.textLabel.text = #"Load More";
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.imageView.image = nil;
return cell;
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(showLoadMore{
return [_my_topics_data_array count]+1;
}
else
{
return [_my_topics_data_array count];
}
}
just now,i suddenly got the answer.
I write 2 CONSTANT:
#define IS_LAST_MY_TOPIC_ROW (indexPath.row == [_my_topics_data_array count] - 1) && [_my_topics_data_array count] != 0
#define IS_LAST_REC_TOPIC_ROW (indexPath.row == [_rec_topics_data_array count] - 1) && [_rec_topics_data_array count] != 0
and in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if(SEGMENTINDEX == 0){
if(IS_LAST_MY_TOPIC_ROW){
cell.textLabel.text = #"Load More";
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.imageView.image = nil;
return cell;
}
topic = [_my_topics_data_array objectAtIndex:indexPath.row];
}else{
if(IS_LAST_REC_TOPIC_ROW){
cell.textLabel.text = #"加载更多";
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.imageView.image = nil;
return cell;
}
topic = [_rec_topics_data_array objectAtIndex:indexPath.row];
}
I am displaying certain information in a tableview. These are basically Exam rooms. I am using this logic in order to put the check marks for selecting the particular room. The code is as follows:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
// intializing tableview cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
// Setting tableviewcell title from Room array.
cell.textLabel.text = [NSString stringWithFormat:#"%#",[[arr_roomList
objectAtIndex:indexPath.row] valueForKey:#"description"]];
/* checking the condition if checkedIndexPath is != null.
means at first time checkedIndexPath= null.*/
if (self.checkedIndexPath) {
/* checking the condition if current cell is selected then
we have to show the UITableViewCellAccessoryCheckmark (checkmark on right side of the
cell).*/
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
else{
if ([[[arr_roomList objectAtIndex:indexPath.row] valueForKey:#"resource_id"]
isEqualToString:self.str_selected_resourceId]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
// This Method will set the Background and Selected Background Image for the cell.
[[AppDelegate sharedInstance] SetBackgroundImageForcell:cell];
if ([[[arr_roomList objectAtIndex:indexPath.row] valueForKey:#"rec_type"] isEqualToString:#"R"]) {
cell.backgroundColor = [UIColor grayColor];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
/* checking the condition if checkedIndexPath is != null.
means at first time checkedIndexPath= null.*/
if(self.checkedIndexPath)
{
UITableViewCell* uncheckCell = [tableView
cellForRowAtIndexPath:self.checkedIndexPath];
uncheckCell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.checkedIndexPath = indexPath;
[self changeRoomWithResourceId:[[arr_roomList objectAtIndex:indexPath.row]
valueForKey:#"resource_id"]];
}
As I am scrolling down the table the check marks are repeating themselves for any cell randomly. Please Help me as this issue is taking lot of time for me.
You have to clear out any checkmarks that might be placed already, because UITableView reuses cells and does not do it automatically:
// ...
if ([[[arr_roomList objectAtIndex:indexPath.row] valueForKey:#"resource_id"]
isEqualToString:self.str_selected_resourceId]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
// add this
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
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 have one tableview with 1 section and two rows,
and down I have one button action.
In the button action I have to check which cell is clicked because according to that cell I have to open email view for sending mail.
I created for two bool values, but It is not working properly, still my emailview get call without cell is selected
this code what i am doing
#implementation View
#synthesize Mytableview,selectedIndexPaths,value1,value2,cellselected;
BOOL values[1];
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [Mytableview dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
if ([indexPath row] == 0 && [indexPath section] == 0)
{
cell.textLabel.text= #"test";
if (values[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
value1=TRUE;
}
else if ([indexPath row] == 1 && [indexPath section] == 0)
{
cell.textLabel.text= #"test";
value2=TRUE;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//[Mytableview deselectRowAtIndexPath:indexPath animated:YES];
//self.cellselected = indexPath.row;
UITableViewCell *cell = [Mytableview cellForRowAtIndexPath:indexPath];
//here values is i give as array on top as BOOL values[1];i give size for it
values[indexPath.row] = !values[indexPath.row];
if (values[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
-(IBAction)ExportEmail
{
if (value1==FALSE) {
return;
}
else {
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
}
}
}
Declare class veriable NSIndexPath;
NSIndexPath *myIndexPath;
Modify your didSelectRowAtIndexPath to below
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [Mytableview cellForRowAtIndexPath:indexPath];
//here values is i give as array on top as BOOL values[1];i give size for it
myIndexPath = indexPath;
if (values[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
You can use that NSIndexPath in you Button's IBAction and according to index you can open up email.
-(IBAction)ExportEmail
{
if(myIndexPath.row==0){
// send mail
}else if(myIndexPath.row==1){
// send mail
}
}
I have a viewcontroller with a nib - which has a tableview, searchbar and searchdisplaycontroller inside.
The searchdisplaycontroller's contentscontroller, results delegate, delegate and results datasource is wired to the viewcontroller in IB. The searchbar's delegate is wired to the view controller in IB. The main tableview is also wired to the viewcontroller and its loading items during runtime properly.
Everything seems to work except the resultstableview - it never gets populated and the tableView:cellForRowAtIndexPath: never gets called AFTER the user types into the searchbar - it only gets called to populate the main tableview.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell o f the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if (tableView.tag == 2)
{
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
// Configure the cell.
NSHotel *selectedHotel = [[self hotelList] objectAtIndex:[indexPath row]];
//Store the hotel id for use later
selectedHotel.id = [indexPath row];
//cell.textLabel.text = [NSString stringWithFormat:#"Catalonia Majorica", indexPath.row];
cell.textLabel.text = [selectedHotel name];
}
else if (tableView.tag == 1)
{
cell.accessoryType = UITableViewCellAccessoryNone;
// Configure the cell...
NSAirport *selectedAirport = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
selectedAirport = [self.filteredListContent objectAtIndex:indexPath.row];
}
else
{
selectedAirport = [self.listContent objectAtIndex:indexPath.row];
}
//Set the label of the cell formatting the distance from the device
cell.textLabel.text = [NSString stringWithFormat:#"%# - %# miles",
selectedAirport.name, [self formattedStringWithDecimal:selectedAirport.milesFromDevice]];
//Adjust the front of the label
cell.textLabel.font = [UIFont boldSystemFontOfSize:12];
}
return cell;
}
Fixed it - it was returning 0 here:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
return [self.filteredListContent count];
}
else if (tableView.tag == 1)
{
return [self.listContent count];
}
else if (tableView.tag == 2)
{
// Return the number of rows in the section.
return [[self hotelList] count];
}
NSLog(#"The rows in tableview!");
return 0;
}