I have 2 sections and 3 rows each as follow:
SEARCH DISTANCE
250 feet
1000 feet
4000 feet
MAP TYPE
Standard
Satellite
Hybrid
I want to have one check mark for a row per section but my current code will uncheck all visible cells for the entire table view and leave one selected row with check mark. In other word, I will have one check mark for the entire table (2 sections). Here I posted my entire code. I have google'd many but there seems none to resolve my problem. Anyone, please help correct my code. Thanks in advance.
#import "PAWSettingsViewController.h"
#import "PAWAppDelegate.h"
#import <Parse/Parse.h>
#interface PAWSettingsViewController ()
- (NSString *)distanceLabelForCell:(NSIndexPath *)indexPath;
- (PAWLocationAccuracy)distanceForCell:(NSIndexPath *)indexPath;
- (NSString *)maptypeLabelForCell:(NSIndexPath *)indexPath;
- (PAWMaptypeSelect)maptypeForCell:(NSIndexPath *)indexPath;
#property (nonatomic, assign) CLLocationAccuracy filterDistance;
#end
typedef enum {
kPAWSettingsTableViewDistance = 0,
kPAWSettingsTableViewMaptype,
kPAWSettingsTableViewNumberOfSections
} kPAWSettingsTableViewSections;
typedef enum {
kPAWSettingsTableViewDistanceSection250FeetRow = 0,
kPAWSettingsTableViewDistanceSection1000FeetRow,
kPAWSettingsTableViewDistanceSection4000FeetRow,
kPAWSettingsTableViewDistanceNumberOfRows
} kPAWSettingsTableViewDistanceSectionRows;
typedef enum {
kPAWSettingsTableViewMaptypeSectionStandardRow = 0,
kPAWSettingsTableViewMaptypeSectionSatelliteRow,
kPAWSettingsTableViewMaptypeSectionHybridRow,
kPAWSettingsTableViewMaptypeNumberOfRows
} kPAWSettingsTableViewMaptypeSectionRows;
#implementation PAWSettingsViewController
#synthesize tableView;
#synthesize filterDistance;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
PAWAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
self.filterDistance = appDelegate.filterDistance;
}
return self;
}
#pragma mark - Custom setters
// Always fault our filter distance through to the app delegate. We just cache it locally because it's used in the tableview's cells.
- (void)setFilterDistance:(CLLocationAccuracy)aFilterDistance {
PAWAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.filterDistance = aFilterDistance;
filterDistance = aFilterDistance;
}
#pragma mark - View lifecycle
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Private helper methods
- (NSString *)distanceLabelForCell:(NSIndexPath *)indexPath {
NSString *cellText = nil;
switch (indexPath.row) {
case kPAWSettingsTableViewDistanceSection250FeetRow:
cellText = #"250 feet";
break;
case kPAWSettingsTableViewDistanceSection1000FeetRow:
cellText = #"1000 feet";
break;
case kPAWSettingsTableViewDistanceSection4000FeetRow:
cellText = #"4000 feet";
break;
case kPAWSettingsTableViewDistanceNumberOfRows: // never reached.
default:
cellText = #"The universe";
break;
}
return cellText;
}
- (PAWLocationAccuracy)distanceForCell:(NSIndexPath *)indexPath {
PAWLocationAccuracy distance = 0.0;
switch (indexPath.row) {
case kPAWSettingsTableViewDistanceSection250FeetRow:
distance = 250;
break;
case kPAWSettingsTableViewDistanceSection1000FeetRow:
distance = 1000;
break;
case kPAWSettingsTableViewDistanceSection4000FeetRow:
distance = 4000;
break;
case kPAWSettingsTableViewDistanceNumberOfRows: // never reached.
default:
distance = 10000 * kPAWFeetToMiles;
break;
}
return distance;
}
- (NSString *)maptypeLabelForCell:(NSIndexPath *)indexPath {
NSString *cellText = nil;
switch (indexPath.row) {
case kPAWSettingsTableViewMaptypeSectionStandardRow:
cellText = #"Standard";
break;
case kPAWSettingsTableViewMaptypeSectionSatelliteRow:
cellText = #"Satellite";
break;
case kPAWSettingsTableViewMaptypeSectionHybridRow:
cellText = #"Hybrid";
break;
case kPAWSettingsTableViewMaptypeNumberOfRows: // never reached.
default:
cellText = #"?";
break;
}
return cellText;
}
- (PAWMaptypeSelect)maptypeForCell:(NSIndexPath *)indexPath {
PAWMaptypeSelect maptype = nil;
switch (indexPath.row) {
case kPAWSettingsTableViewMaptypeSectionStandardRow:
maptype = #"Standard";
break;
case kPAWSettingsTableViewMaptypeSectionSatelliteRow:
maptype = #"Satellite";
break;
case kPAWSettingsTableViewMaptypeSectionHybridRow:
maptype = #"Hybrid";
break;
case kPAWSettingsTableViewMaptypeNumberOfRows: // never reached.
default:
maptype = nil;
break;
}
return maptype;
}
#pragma mark - UINavigationBar-based actions
- (IBAction)done:(id)sender {
[self.presentingViewController dismissModalViewControllerAnimated:YES];
}
#pragma mark - UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return kPAWSettingsTableViewNumberOfSections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch ((kPAWSettingsTableViewSections)section) {
case kPAWSettingsTableViewDistance:
return kPAWSettingsTableViewDistanceNumberOfRows;
break;
case kPAWSettingsTableViewMaptype:
return kPAWSettingsTableViewMaptypeNumberOfRows;
break;
case kPAWSettingsTableViewNumberOfSections:
return 2;
break;
};
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"SettingsTableView";
if (indexPath.section == kPAWSettingsTableViewDistance) {
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];
if ( cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier];
}
// Configure the cell.
cell.textLabel.text = [self distanceLabelForCell:indexPath];
if (self.filterDistance == 0.0) {
NSLog(#"We have a zero filter distance!");
}
PAWLocationAccuracy filterDistanceInFeet = self.filterDistance * ( 1 / kPAWFeetToMeters);
PAWLocationAccuracy distanceForCell = [self distanceForCell:indexPath];
if (abs(distanceForCell - filterDistanceInFeet) < 0.001 ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
else if (indexPath.section == kPAWSettingsTableViewMaptype){
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];
if ( cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier];
}
// Configure the cell.
cell.textLabel.text = [self maptypeLabelForCell:indexPath];
return cell;
}
else {
return nil;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch ((kPAWSettingsTableViewSections)section) {
case kPAWSettingsTableViewDistance:
return #"Search Distance";
break;
case kPAWSettingsTableViewMaptype:
return #"Map Type";
break;
case kPAWSettingsTableViewNumberOfSections:
return #"";
break;
}
}
#pragma mark - UITableViewDelegate methods
// Called after the user changes the selection.
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == kPAWSettingsTableViewDistance) {
[aTableView deselectRowAtIndexPath:indexPath animated:YES];
// if we were already selected, bail and save some work.
UITableViewCell *selectedCell = [aTableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark) {
return;
}
// uncheck all visible cells.
for (UITableViewCell *cell in [aTableView visibleCells]) {
if (cell.accessoryType != UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
PAWLocationAccuracy distanceForCellInFeet = [self distanceForCell:indexPath];
self.filterDistance = distanceForCellInFeet * kPAWFeetToMeters;
}
else if (indexPath.section == kPAWSettingsTableViewMaptype){
[aTableView deselectRowAtIndexPath:indexPath animated:YES];
// if we were already selected, bail and save some work.
UITableViewCell *selectedCell = [aTableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark) {
return;
}
// uncheck all visible cells.
for (UITableViewCell *cell in [aTableView visibleCells]) {
if (cell.accessoryType != UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
PAWMaptypeSelect maptypeForCell = [self maptypeForCell:indexPath];
}
}
#end
you have an error with this
// uncheck all visible cells.
for (UITableViewCell *cell in [aTableView visibleCells]) {
if (cell.accessoryType != UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
Here the for loop deselect all the cell. you have to check also the section in if loop
you can set tag for your cell like this
cell.tag = indexPath.section;
and in for loop if condition is
for (UITableViewCell *cell in [aTableView visibleCells]) {
if (cell.accessoryType != UITableViewCellAccessoryNone && cell.tag == indexPath.section) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
problem in your code is that You only checkmark row for section 0 by replace below method you can solved your problem.
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"SettingsTableView";
if (indexPath.section == kPAWSettingsTableViewDistance) {
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];
if ( cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier];
}
// Configure the cell.
cell.textLabel.text = [self distanceLabelForCell:indexPath];
if (self.filterDistance == 0.0) {
NSLog(#"We have a zero filter distance!");
}
PAWLocationAccuracy filterDistanceInFeet = self.filterDistance * ( 1 / kPAWFeetToMeters);
PAWLocationAccuracy distanceForCell = [self distanceForCell:indexPath];
if (abs(distanceForCell - filterDistanceInFeet) < 0.001 ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
else if (indexPath.section == kPAWSettingsTableViewMaptype){
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];
if ( cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier];
}
// Configure the cell.
cell.textLabel.text = [self maptypeLabelForCell:indexPath];
PAWLocationAccuracy filterDistanceInFeet = self.filterDistance * ( 1 / kPAWFeetToMeters);
PAWLocationAccuracy distanceForCell = [self distanceForCell:indexPath];
if (abs(distanceForCell - filterDistanceInFeet) < 0.001 ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
else {
return nil;
}
}
For now I have found the following bugs
You don't save which cell is selected for map type.
The way how you checkmark your cell is incorrect.
In switch you don't have to use break after return statement
In case kPAWSettingsTableViewNumberOfSections: you should return kPAWSettingsTableViewNumberOfSections
First two defects causes many boilerplate code. I think you should store the NSIndexPath's for each section that is selected as additional #property. Instead of unchecking all and checking tapped, you can do in tableView:didSelectRowAtIndexPath::
Determine which one should be selected.
Store local old value of your selectedIndexPath.
Change selectedIndexPath proprty to new one.
Reload both rows.
Then you have to add in tableView:cellForRowAtIndexPath: the following code:
if (indexPath.row == self.selectedIndexPath.row && indexPath.section == self.selectedIndexPath.section) {
// Change to checkmark.
} else {
// Change to not checked.
}
I got very similar way to checkmark cell in my own project. I hope the code below also will help you understand my way to fix your problem:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (/*condition*/) {
[...]
}
else if (/*checkmark section*/) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckmarkCellId forIndexPath:indexPath];
cell.textLabel.text = [self labelForRow:indexPath.row];
cell.accessoryType = (indexPath.row == [self defaultAgglomerationIndex]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
cell.imageView.image = (indexPath.row == [self selectedAgglomerationIndex]) ? [UIImage imageNamed:#"icoMap"] : [UIImage imageNamed:#"region"];
return cell;
}
else if (/*Another condition */) {
[...]
}
return nil;
}
And essencial code in tableView:didSelectRowAtIndexPath: (I reload whole section)
self.selectedIndex = indexPath;
NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
[self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
Try this, worked just fine for me:
-VARIABLES:
#property NSInteger checkedCellRow;
#property NSInteger checkedCellSection;
-FUNCTIONS:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (checkedCellSection == indexPath.section)
{
if (checkedCellRow == indexPath.row)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
checkedCellRow = indexPath.row;
checkedCellSection = indexPath.section;
[myTable reloadData];
}
Related
This is a follow-up question to UITableViewCell - check mark one row per section.
Here I have 2 sections with 3 rows each as follow:
SEARCH DISTANCE
250 feet
1000 feet
4000 feet
MAP TYPE
Standard
Satellite
Hybrid
I have a problem in section2(MAP TYPE). I want to have 'standard' checked as default and whatever option user chooses later on can be saved and passed on to another view controller which contains map view to display according to map type selected. My current code does not allow me to do so for section2. Anyone, please help check my code and suggest me what need to be added/modified?
I've been trying to learn how it works for section1(SEARCH DISTANCE) but I just don't quite get it. It seems to involve delegate file to do cache things.
Here my code is:
#import "PAWSettingsViewController.h"
#import "PAWAppDelegate.h"
#import <Parse/Parse.h>
#interface PAWSettingsViewController ()
- (NSString *)distanceLabelForCell:(NSIndexPath *)indexPath;
- (PAWLocationAccuracy)distanceForCell:(NSIndexPath *)indexPath;
- (NSString *)maptypeLabelForCell:(NSIndexPath *)indexPath;
- (PAWMaptypeSelect)maptypeForCell:(NSIndexPath *)indexPath;
#property (nonatomic, assign) CLLocationAccuracy filterDistance;
#end
typedef enum {
kPAWSettingsTableViewDistance = 0,
kPAWSettingsTableViewMaptype,
kPAWSettingsTableViewNumberOfSections
} kPAWSettingsTableViewSections;
typedef enum {
kPAWSettingsTableViewDistanceSection250FeetRow = 0,
kPAWSettingsTableViewDistanceSection1000FeetRow,
kPAWSettingsTableViewDistanceSection4000FeetRow,
kPAWSettingsTableViewDistanceNumberOfRows
} kPAWSettingsTableViewDistanceSectionRows;
typedef enum {
kPAWSettingsTableViewMaptypeSectionStandardRow = 0,
kPAWSettingsTableViewMaptypeSectionSatelliteRow,
kPAWSettingsTableViewMaptypeSectionHybridRow,
kPAWSettingsTableViewMaptypeNumberOfRows
} kPAWSettingsTableViewMaptypeSectionRows;
#implementation PAWSettingsViewController
#synthesize tableView;
#synthesize filterDistance;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
PAWAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
self.filterDistance = appDelegate.filterDistance;
}
return self;
}
#pragma mark - Custom setters
// Always fault our filter distance through to the app delegate. We just cache it locally because it's used in the tableview's cells.
- (void)setFilterDistance:(CLLocationAccuracy)aFilterDistance {
PAWAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.filterDistance = aFilterDistance;
filterDistance = aFilterDistance;
}
#pragma mark - View lifecycle
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Private helper methods
- (NSString *)distanceLabelForCell:(NSIndexPath *)indexPath {
NSString *cellText = nil;
switch (indexPath.row) {
case kPAWSettingsTableViewDistanceSection250FeetRow:
cellText = #"250 feet";
break;
case kPAWSettingsTableViewDistanceSection1000FeetRow:
cellText = #"1000 feet";
break;
case kPAWSettingsTableViewDistanceSection4000FeetRow:
cellText = #"4000 feet";
break;
case kPAWSettingsTableViewDistanceNumberOfRows: // never reached.
default:
cellText = #"The universe";
break;
}
return cellText;
}
- (PAWLocationAccuracy)distanceForCell:(NSIndexPath *)indexPath {
PAWLocationAccuracy distance = 0.0;
switch (indexPath.row) {
case kPAWSettingsTableViewDistanceSection250FeetRow:
distance = 250;
break;
case kPAWSettingsTableViewDistanceSection1000FeetRow:
distance = 1000;
break;
case kPAWSettingsTableViewDistanceSection4000FeetRow:
distance = 4000;
break;
case kPAWSettingsTableViewDistanceNumberOfRows: // never reached.
default:
distance = 10000 * kPAWFeetToMiles;
break;
}
return distance;
}
- (NSString *)maptypeLabelForCell:(NSIndexPath *)indexPath {
NSString *cellText = nil;
switch (indexPath.row) {
case kPAWSettingsTableViewMaptypeSectionStandardRow:
cellText = #"Standard";
break;
case kPAWSettingsTableViewMaptypeSectionSatelliteRow:
cellText = #"Satellite";
break;
case kPAWSettingsTableViewMaptypeSectionHybridRow:
cellText = #"Hybrid";
break;
case kPAWSettingsTableViewMaptypeNumberOfRows: // never reached.
default:
cellText = #"?";
break;
}
return cellText;
}
- (PAWMaptypeSelect)maptypeForCell:(NSIndexPath *)indexPath {
PAWMaptypeSelect maptype = nil;
switch (indexPath.row) {
case kPAWSettingsTableViewMaptypeSectionStandardRow:
maptype = #"Standard";
break;
case kPAWSettingsTableViewMaptypeSectionSatelliteRow:
maptype = #"Satellite";
break;
case kPAWSettingsTableViewMaptypeSectionHybridRow:
maptype = #"Hybrid";
break;
case kPAWSettingsTableViewMaptypeNumberOfRows: // never reached.
default:
maptype = nil;
break;
}
return maptype;
}
#pragma mark - UINavigationBar-based actions
- (IBAction)done:(id)sender {
[self.presentingViewController dismissModalViewControllerAnimated:YES];
}
#pragma mark - UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return kPAWSettingsTableViewNumberOfSections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch ((kPAWSettingsTableViewSections)section) {
case kPAWSettingsTableViewDistance:
return kPAWSettingsTableViewDistanceNumberOfRows;
break;
case kPAWSettingsTableViewMaptype:
return kPAWSettingsTableViewMaptypeNumberOfRows;
break;
case kPAWSettingsTableViewNumberOfSections:
return 0;
break;
};
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"SettingsTableView";
if (indexPath.section == kPAWSettingsTableViewDistance) {
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];
if ( cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier];
}
// Configure the cell.
cell.tag = indexPath.section;
cell.textLabel.text = [self distanceLabelForCell:indexPath];
if (self.filterDistance == 0.0) {
NSLog(#"We have a zero filter distance!");
}
PAWLocationAccuracy filterDistanceInFeet = self.filterDistance * ( 1 / kPAWFeetToMeters);
PAWLocationAccuracy distanceForCell = [self distanceForCell:indexPath];
if (abs(distanceForCell - filterDistanceInFeet) < 0.001 ) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
} else if (indexPath.section == kPAWSettingsTableViewMaptype){
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];
if ( cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier];
}
// Configure the cell.
cell.tag = indexPath.section;
cell.textLabel.text = [self maptypeLabelForCell:indexPath];
//**** I don't know how to do here? ****
return cell;
} else {
return nil;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch ((kPAWSettingsTableViewSections)section) {
case kPAWSettingsTableViewDistance:
return #"Search Distance";
break;
case kPAWSettingsTableViewMaptype:
return #"Map Type";
break;
case kPAWSettingsTableViewNumberOfSections:
return #"";
break;
}
}
#pragma mark - UITableViewDelegate methods
// Called after the user changes the selection.
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == kPAWSettingsTableViewDistance) {
[aTableView deselectRowAtIndexPath:indexPath animated:YES];
// if we were already selected, bail and save some work.
UITableViewCell *selectedCell = [aTableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark) {
return;
}
// uncheck all visible cells.
for (UITableViewCell *cell in [aTableView visibleCells]) {
if (cell.accessoryType != UITableViewCellAccessoryNone && cell.tag == indexPath.section) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
PAWLocationAccuracy distanceForCellInFeet = [self distanceForCell:indexPath];
self.filterDistance = distanceForCellInFeet * kPAWFeetToMeters;
}
else if (indexPath.section == kPAWSettingsTableViewMaptype){
[aTableView deselectRowAtIndexPath:indexPath animated:YES];
// if we were already selected, bail and save some work.
UITableViewCell *selectedCell = [aTableView cellForRowAtIndexPath:indexPath];
if (selectedCell.accessoryType == UITableViewCellAccessoryCheckmark) {
return;
}
// uncheck all visible cells.
for (UITableViewCell *cell in [aTableView visibleCells]) {
if (cell.accessoryType != UITableViewCellAccessoryNone && cell.tag == indexPath.section) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
//**** I don't know how to do here? ****
}
}
#end
Use an bool variable. Like isFirstTime.
and then in viewDidLoad: initilize your isFirstTime variable as YES.
isFirstTime = YES;
in your
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
update this part
else if (indexPath.section == kPAWSettingsTableViewMaptype){
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];
if ( cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier];
}
if ( isFirstTime == YES && indexPath.row == 0)
{
} // Configure the cell.
cell.tag = indexPath.section;
cell.textLabel.text = [self maptypeLabelForCell:indexPath];
//**** I don't know how to do here? ****
return cell;
}
I have figured out my own question. I decided to go for global variable to save selected choice in section2.
I declared global variable in .m file:
int gmaptypeSelected;
In cellForRowAtOIndexPath section I put a condition like this:
else if (indexPath.section == kPAWSettingsTableViewMaptype){
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:identifier];
if ( cell == nil )
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:identifier];
}
// Configure the cell.
cell.tag = indexPath.section;
cell.textLabel.text = [self maptypeLabelForCell:indexPath];
MKMapType maptypeForCell = [self maptypeForCell:indexPath];
if (maptypeForCell == gmaptypeSelected) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
And in MapView Controller > viewWillAppear will look like this:
- (void)viewWillAppear:(BOOL)animated {
extern int gmaptypeSelected;
if (gmaptypeSelected == 0) {
self.mapView.mapType = MKMapTypeStandard;
} else if (gmaptypeSelected == 1) {
self.mapView.mapType = MKMapTypeSatellite;
} else if (gmaptypeSelected == 2) {
self.mapView.mapType = MKMapTypeHybrid;
}
[locationManager startUpdatingLocation];
[super viewWillAppear:animated];
}
I'm still very new to objective-c, and I've looked all for an answer. I haven't been able to successfully implement anything I've found yet. I'm using an open source sliding menu that I found, and I haven't found anything that fits within the code it uses. (I'd post a link to it, but I can't remember where I found it.)
EDIT: I've got the sections added. Now, I just need to make it so the first cell in the second section only shows up in the first section. http://i.imgur.com/gSIRNJ3.png
I've updated the code below. Here's a reference of what I'm trying to do. Right now, cell 1 shows up in both section 1 and section 2.
SECTION 1: (no header)
- cell 1: "Search Nouns"
SECTION 2: "Noun Conjugation"
- cell 2: "Regular Nouns"
- cell 3: "Irregular Nouns"
- cell 4: "Uncountable Nouns"
#import "BaseTableViewController.h"
#import "KYSlideMenuViewController.h"
#interface BaseTableViewController () {
int _currentCtrNum;
}
#end
#implementation BaseTableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// 最初に表示するビューを設定する
_currentCtrNum = -1;
NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
[self tableView:self.tableView didSelectRowAtIndexPath:path];
}
#pragma mark -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
return 1;
else
return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(section==0)
return 0;
else
return 25;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (section == 0)
return #"";
else
return #"reference";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"名詞検索";
break;
case 1:
cell.textLabel.text = #"Side B";
break;
case 2:
cell.textLabel.text = #"Side C";
break;
case 3:
cell.textLabel.text = #"Side D";
default:
break;
}
cell.backgroundColor = [UIColor grayColor];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
for (int i = 0 ; i < [tableView numberOfRowsInSection:0] ; i++) {
NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path];
if (i == indexPath.row) {
cell.textLabel.textColor = [UIColor whiteColor];
} else {
cell.textLabel.textColor = [UIColor blackColor];
}
}
// 現在のビューコントローラなら表示するだけ
if (_currentCtrNum == indexPath.row) {
[(KYSlideMenuViewController *)self.parentViewController slideChildTo:KYS_CLOSE];
return;
}
_currentCtrNum = (int)indexPath.row;
// 新しいビューコントローラを生成してコンテナのビューコントローラを入れ替え
UIViewController *toCtr;
switch (indexPath.row) {
case 0: // noun search
toCtr = [[self storyboard] instantiateViewControllerWithIdentifier:#"itemTableView"];
break;
case 1: // navigationあり
toCtr = [[self storyboard] instantiateViewControllerWithIdentifier:#"mainView"];
break;
case 2: // navigationなし
toCtr = [[self storyboard] instantiateViewControllerWithIdentifier:#"subView"];
break;
case 3: // navigationあり
toCtr = [[self storyboard] instantiateViewControllerWithIdentifier:#"srdView"];
break;
default:
return;
}
[(KYSlideMenuViewController *)self.parentViewController changeTo:toCtr];
}
#end
There also supposed to be a delegate for numberOfSectionsInTableView: where you would return 2;
I am working with the tableview and working well with that. But i stuck at one place where i want to tap the cell and on the tap the cell should expand its height and and the cell below it should move down according to the expanded height.
The code which i am using is as follows.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.textLabel setNumberOfLines:0]; // unlimited number of lines
gratitude *grat = [gratitude_list objectAtIndex:indexPath.row];
cell.textLabel.text=grat.gratitude;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[cell.textLabel setFont:[UIFont fontWithName:#"Lucida Sans" size:20]];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
gratindex > -1;
if (gratindex == indexPath.row) {
[self collapseSubItemsAtIndex:indexPath];
gratindex = -1;
}
else {
BOOL shouldCollapse = gratindex > -1;
if (shouldCollapse) {
[self collapseSubItemsAtIndex:gratindex];
}
gratindex = (shouldCollapse && indexPath.row > gratindex) ? indexPath.row - [[gratitude_list objectAtIndex:gratitude_list] count] : indexPath.row;
[self expandItemAtIndex:gratitude_list];
}
[self.gratitude_tableview endUpdates];
}
- (void)expandItemAtIndex:(int)index
{
NSMutableArray *indexPaths = [NSMutableArray new];
int insertPos = index + 1;
for (int i = 0; i < [gratitude_list count]; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:insertPos++ inSection:0]];
}
[self.gratitude_tableview insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.gratitude_tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (void)collapseSubItemsAtIndex:(int)index {
NSMutableArray *indexPaths = [NSMutableArray new];
for (int i = index + 1; i <= index + [[gratitude_list objectAtIndex:index] count]; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[self.gratitude_tableview deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
}
Please help me out if i am using wrong approch please help me out.
Please check my answer. If you need any more detailing then you can ask me in comment.
UPDATED:
ANd in .h file
#interface tableViewDemoViewController : UITableViewController
{
NSIndexPath *selectedCellIndexPath;
BOOL clickedAgain;
int heights;
}
And in .m file
#import "tableViewDemoViewController.h"
#interface tableViewDemoViewController ()
#end
#implementation tableViewDemoViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
clickedAgain = YES;
heights =0;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return 15;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
cell.textLabel.text = [NSString stringWithFormat:#"This is cell %d",indexPath.row ];
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
if ([selectedCellIndexPath compare:indexPath] == NSOrderedSame && !clickedAgain)
{
heights =40;
clickedAgain =YES;
}
else
{
heights =80;
clickedAgain = NO;
}
[tableView beginUpdates];
selectedCellIndexPath = indexPath;
[tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if(selectedCellIndexPath != nil
&& [selectedCellIndexPath compare:indexPath] == NSOrderedSame )
{
return heights;
}
return 40;
} #end
I'm trying to allow only one UITableViewCell to be checked at a time in a section. What I'm going for is to uncheck a checked UITableViewCell (if there is one) and checking the current selected cell. So basically, If one CellA is selected, and I select CellB, I want CellA to unselect and CellB to select.
Here's what I've done to try and accomplish this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(![self.selectedIndexPaths containsObject:indexPath]) {
[self.selectedIndexPaths addObject:indexPath];
}
else {
[self.selectedIndexPaths removeObject:indexPath];
}
if (indexPath.section == 0) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
else if (indexPath.section == 1) {
if (![self.selectedIndexPaths containsObject:indexPath]) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
else {
for (NSIndexPath *indexPath2 in self.selectedIndexPaths) {
if (indexPath2.section == 1) {
[tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden = YES;
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
}
}
}
else if (indexPath.section == 2) {
if (![self.selectedIndexPaths containsObject:indexPath]) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
else {
for (NSIndexPath *indexPath2 in self.selectedIndexPaths) {
if (indexPath2.section == 2) {
[tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden = YES;
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
}
}
}
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
if ([tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == YES) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
else {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = YES;
}
}
else if (indexPath.section == 1) {
if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == YES) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
else if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == NO){
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = YES;
}
else {
for (NSIndexPath *indexPath2 in self.selectedIndexPaths) {
if (indexPath2.section == 1 && [tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden == YES) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
}
}
}
else if (indexPath.section == 2) {
if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == YES) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
else if (![self.selectedIndexPaths containsObject:indexPath] && [tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden == NO){
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = YES;
}
else {
for (NSIndexPath *indexPath2 in self.selectedIndexPaths) {
if (indexPath2.section == 2 && [tableView cellForRowAtIndexPath:indexPath2].accessoryView.hidden == YES) {
[tableView cellForRowAtIndexPath:indexPath].accessoryView.hidden = NO;
}
}
}
}
}
As you can see, I have three sections, and the first one the user can select however many cells they want, but in the second and third, it's limited to one.
The Issue: Whenever I select a cell and then another cell, it works. If I select the first cell, both of them are now checked.
I would appreciate any help or suggestions. Thanks!
Your code looks a lot more complicated than it needs to be. I would do it by creating a property (of type NSIndexPath) for sections 1 and 2, and an array property for section 0 to keep track of the selected cell or cells in the respective sections. Set the value of the properties with the indexPath, or add the indexPath in the case of the array, when a cell is tapped (or delete it if it's already checked). Then check the state of those properties or array in cellForRowAtIndexPath.
#interface TableController ()
#property (strong,nonatomic) NSArray *theData;
#property (strong,nonatomic) NSIndexPath *selectedPathForSection1;
#property (strong,nonatomic) NSIndexPath *selectedPathForSection2;
#property (strong,nonatomic) NSMutableArray *selectedPaths;
#end
#implementation TableController
- (void)viewDidLoad {
[super viewDidLoad];
self.selectedPaths = [NSMutableArray new];
self.selectedPathForSection1 = nil;
self.selectedPathForSection2 = nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = self.theData[indexPath.section][indexPath.row];
if ([self.selectedPathForSection1 isEqual:indexPath] || [self.selectedPathForSection2 isEqual:indexPath] || [self.selectedPaths containsObject:indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.section) {
case 0:
if (! [self.selectedPaths containsObject:indexPath]) {
[self.selectedPaths addObject:indexPath];
}else{
[self.selectedPaths removeObject:indexPath];
}
break;
case 1:
if (![self.selectedPathForSection1 isEqual:indexPath]) {
self.selectedPathForSection1 = indexPath;
}else{
self.selectedPathForSection1 = nil;
}
break;
case 2:
if (![self.selectedPathForSection2 isEqual:indexPath]) {
self.selectedPathForSection2 = indexPath;
}else{
self.selectedPathForSection2 = nil;
}
break;
}
[tableView reloadData];
}
I have searched and search and i just can´t seem to find an answer to my problem. I have a dynamic tableview with 3 rows (each row is a section) and a edit button at the top right of the tableview. each time the user taps edit it has to be possible to add or delete a row. Everything works except the part when the + button is taped to ad a new row. This is my code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count = [myarray count];
if (myarray != nil) count ++;
return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
id cell;
switch(indexPath.section) {
case 0:
if(indexPath.row==0) {
static NSString *cellType1 = #"cellType1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellType1];
return cell;
}
break;
case 1:
if(indexPath.row==0) {
static NSString *cellType2= #"cellType2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellType2];
return cell;
}
break;
case 2:
if(indexPath.row==0) {
static NSString *cellType3= #"cellType3";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellType3];
return cell;
}
break;
default:
break;
}
return cell;
}
- (IBAction) EditTable:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[Table setEditing:NO animated:NO];
[Table reloadData];
[self.navigationItem.rightBarButtonItem setTitle:#"Edit"];
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[Table setEditing:YES animated:YES];
[Table reloadData];
[self.navigationItem.rightBarButtonItem setTitle:#"Ok"];
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;
if (self.editing && indexPath.row == ([myarray count]))
{
return UITableViewCellEditingStyleInsert;
} else
{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)TableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[myarray removeObjectAtIndex:indexPath.row];
[Table reloadData];
} else if (editingStyle == UITableViewCellEditingStyleInsert)
{
switch(indexPath.section) {
case 0:
if(indexPath.row==0) {
static NSString *cellType1 = #"cellType1";
UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:cellType1];
[arry insertObject:cell atIndex:[myarray count]];
[Table reloadData];
}
break;
case 1:
if(indexPath.row==0) {
static NSString *cellType2= #"cellType2";
UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:cellType2];
[arry insertObject:cell atIndex:[myarray count]];
}
break;
case 2:
if(indexPath.row==0) {
static NSString *cellType3= #"cellType3";
UITableViewCell *cell = [TableView dequeueReusableCellWithIdentifier:cellType3];
[arry insertObject:cell atIndex:[myarray count]];
}
break;
default:
break;
}
}
As i said before, everything works until i press the + button (that appears on the left side when the edit button is pressed) to add a new row. Then it shows an error: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath.
What am i doing wrong? any help would be most appreciated.
First off, you never actually +alloc or -init a cell, so -cellForRowAtIndexPath: is most likely returning nil (-dequeueReusableCellWithIdentifier: doesn't cut it).
Second off, comparing indexPath.row to [myarray count] will never be true, because arrays and tables may be zero-based, but their counts aren't.