I am using paging with uitableview. I have array of 40 objects. In the numberofrowsinasection method, I am returning 10 rows. Its mean total number of pages is four. Within the pagecontrol method, I am implementing the paging. When I like
-(ibaction) pagecontrol:
{
if (pagecontrol.currentpage == 0)
{
for (int i=0; i<10; i++ )
{
cell.textlabel.text = [objectarray objectatindex:i];
}
}
if (pagecontrol.currentpage == 1)
{
for (int i=10; i<19; i++ )
{
cell.textlabel.text = [objectarray objectatindex:i];
}
}
if (pagecontrol.currentpage == 2)
{
for (int i=20; i<29; i++ )
{
cell.textlabel.text = [objectarray objectatindex:i];
}
}
if (pagecontrol.currentpage == 3)
{
for (int i=30; i<39; i++ )
{
cell.textlabel.text = [objectarray objectatindex:i];
}
}
}
But I found a strange result in uiableview. Is it correct? Where am I doing the mistake in this pagecontrol method?
Do you always have exactly 40 rows or could there be less?
The code you posted starts with a missing method parameter. There should be either no colon or a parameter after the colon (with a data type).
The code you posted suffers from several problems. The biggest issue is you set the text of the same cell 10 times. You need to set the text of 10 cells, one time each.
Regardless of that, you are going about this all wrong. Replace your posted method with this:
-(IBAction)pagecontrol:(UIPageControl *)pageControl {
[self.tableView reloadData];
}
Then, in your tableView:cellForRowAtIndexPath: method you should do the following:
- (void)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableView *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
int base = pagecontrol.currentPage * 10;
int index = base + indexPath.row;
NSString *text = objectArray[index];
cell.textLabel.text = text;
return cell;
}
This is in addition to your numberOfSectionsInTableView: and tableView:numberOfRowsInSection: methods.
That's all you need.
BTW - case matters. Don't type everything in lowercase. Type it in properly with the proper case.
You are using a constant row in paging to use the section in tableview. It differentiates the rows in the page,
like this:
Set the number of sections in tableview:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 4;
}
// Set rows in section
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch(section){
case 0:
return 10;
break;
case 1:
return 10;
break;
.....
.....
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *FirstLevelCell= #"FirstLevelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell] autorelease];
}
switch([indexPath section]){
case 0:
{
//Add your code
}
break;
case 1:
{
//Add your code
}
break;
.........
.........
.........
.........
.........
}
}
//Reload table view
[self.tableView reloadData];
Related
I am having trouble updating the tableview datasource array.
I want something like this: If user logged in through registration , he will view 13 rows in tableView.
If he logged in using Facebook , he would see 10 rows and as a guest he will see 5 rows.
The thing is tableView's rows are updated successfully but not the actions of tableview cells at different indexpaths.
I am doing something like this:
- (NSInteger) numberOfSectionsInTableView : (UITableView *) tableView
{
// Return the number of sections.
if( [SharedGlobal SharedSingleton].userBool==TRUE)
{
return 5;
}
else if([[[[NSUserDefaults standardUserDefaults]objectForKey:#"profileArrayForGlobal"] valueForKey:#"UserType"] integerValue]==1)
{
return 13;
}
else
{
return 10;
}
}
Now for cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil)
{
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"Cell"];
}
if( [SharedGlobal SharedSingleton].userBool==TRUE){
// do something
else if([[[[NSUserDefaults standardUserDefaults]objectForKey:#"profileArrayForGlobal"] valueForKey:#"UserType"] integerValue]==1)
{
//do something
if ([self tableView:tableView canCollapseSection:indexPath.section])
{
if (!indexPath.row)
{
//do something
if ([expandedSections containsIndex:indexPath.section])
{
// do something
}
else
{
//do something
}
}
else
{
// all other rows
}
}
else
{
cell.accessoryView = nil;
cell.lblMenuName.text= [lableArray objectAtIndex:indexPath.section];
cell.imgMenu.image=[self crop:[UIImage imageNamed: [imageArray objectAtIndex:indexPath.section]]];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
}
else
{
cell.lblMenuName.text= [lableArrayOrg objectAtIndex:indexPath.section];
cell.imgMenu.image=[self crop:[UIImage imageNamed: [imageArrayOrg objectAtIndex:indexPath.section]]];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
return cell;
}
Basically you are writing code in wrong method, as you need to show the rows based on differrent condition, change the method from
(NSInteger) numberOfSectionsInTableView : (UITableView *) tableView
to
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
I have a single array of any count. I want to divide in different section as multiple of 7. I am unable to get this to work. Here is a sample of 2 elements.
- (void)viewDidLoad {
[super viewDidLoad];
array =[NSMutableArray arrayWithObjects:#"d",#"s",#"a",#"qq",#"dqd",#"dqq",#"qdqdf",#"dqdfqf", nil];
// Do any additional setup after loading the view from its nib.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ceil(array.count / 2.0); // round up the floating point division
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger sections = [self numberOfSectionsInTableView:tableView];
if (section == sections - 1) {
NSInteger count = array.count & 2;
if (count == 0) {
count = 2;
}
return count;
} else {
return 2;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
// cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
Your question isn't clear but I think you want 7 rows in every section except the last section which would have just enough for the last remaining rows that don't fit in the rest of the sections.
Assuming this is correct you need to properly calculate the number of sections as follows:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return ceil(array.count / 7.0); // round up the floating point division
}
Now the number of rows in each section will be 7 except for the last section which could have 1 - 7.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger sections = [self numberOfSectionsInTableView:tableView];
if (section == sections - 1) {
NSInteger count = array.count % 7;
if (count == 0) {
count = 7;
}
return count;
} else {
return 7;
}
}
You also need to be able to convert an indexPath into an array index:
NSInteger index = indexPath.section * 7 + indexPath.row;
And you need to be able to convert an array index into an indexPath:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index % 7 inSection:index / 7];
Or, instead of all of this, you could setup your data structures to be an array of arrays. That actually makes your data better match how it will be used by the table.
Update for you revised question:
Your cellForRowAtIndexPath method needs to change:
//cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
to:
NSInteger index = indexPath.section * 7 + indexPath.row;
cell.textLabel.text = tableData[index];
just like I showed above.
So I'm not entirely on point with objective-C as it's been awhile.
But the easiest thing I'd think to do would be a for loop that iterates through the length of the array and at every 7th will divide the array.
Here's a bit of psuedocode.
for(int i =0; i<array.length<i=i+7)
{
//take the first index, take the 7th index.
//split the array from the first index to the 7th
//repeat for all remaining values.
}
I'm not sure if you wanted all the different sections that it could make from the 7 intervals, or only one. If you could clarify I might be able to answer this better.
I'm working with Xcode 6.1.1, and I have a table view controller with static cells having 4 sections.
Section 0: contains two rows, each with 2 labels
Section 1: contains 7 image views
Section 2: contains 1 row with 1 label
Section 3: contains a variable number or rows each containing 2 image
views
I want to load all cells in the table view controller with data.
I gave each different row a reuse identifier (5 reuse identifiers in total), and I also created a class for each of the different kind of cells (5 classes)
I created outlets in the classes, and linked them in the storyboard each to its label/image view.
However I don't know how to proceed next.
If I implement the table view data source functions, I'm getting the correct number of cells, however they are all blank.
Here's how I'm creating the cells:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
if (indexPath.section == 0 && indexPath.row == 0) {
static NSString *CellIdentifier = #"MyCell1";
MyTableViewCell1 *cell = (MyTableViewCell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyTableViewCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
} else {
//create other cells here
}
cell.label1.text=#"testing 123";
return cell;
}
However, I'm always getting cells with default style (my labels or image views are not appearing)
What am I missing?? What code should I write in the custom table view cell classes?
EDIT: After gavin's suggestion, I modified the delegate function to use the dequeueReusableCellWithIdentifier:forIndexPath
I also added: [self.tableView registerClass:[MyTableViewCell1 class] forCellReuseIdentifier:#"MyCell1"] to my viewdidload method.
Here's my final code:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[MyTableViewCell1 class] forCellReuseIdentifier:#"MyCell1"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 3 ) {
return 1;
} else {
return [super tableView:tableView numberOfRowsInSection:section];
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if (indexPath.section == 3) {
// make dynamic row's cell
static NSString *CellIdentifier = #"MyCell1";
MyTableViewCell1 *cell = (MyTableViewCell1*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell configureCellWithValues:#"17" andV2:#"13"];
return cell;
} else {
return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 3) {
MyTableViewCell1 *c = (MyTableViewCell1 *) cell;
NSLog(#"willdisplay cell with values %#, %#",c.i1, c.i2);
[c.image1 setImage:[UIImage imageNamed:#"2"]];
}
}
And here's my custom cell's class:
#implementation MyTableViewCell1
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void) configureCellWithValues: (NSString*) v1 andV2:(NSString*) v2 {
self.i1=v1;
self.i2=v2;
self.image1.image = [UIImage imageNamed:v1];
self.image2.image = [UIImage imageNamed:v2];
}
#end
The cells in section 3 are still not showing the images (not even the imageviews)
Look like you're using the old method of tableView reusable cell dequeueing - the new function accepts an indexPath as well as the cell identifier; it also now guarantees to return a cell (so no need for a nil check).
Here's your delegate function cleaned up:
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
if (indexPath.section == 0 && indexPath.row == 0) {
static NSString *CellIdentifier = #"MyCell1";
MyTableViewCell1 *cell = (MyTableViewCell1*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
} else {
//create other cells here
}
cell.label1.text=#"testing 123";
return cell;
}
Another tip would be to encapsulate your setting of the label values into each individual cell subclass, e.g. add a method called configure to MyTableViewCell1 that sets the label values to what you want displayed.
It seems that we can't generate custom static cells and try to access their outlets and change them (because their static) please correct me if i'm wrong because I couldn't access them at all.
I've solved my Problem by changing the TableView Type to Dynamic Cells, i didn't put any prototype cells in the tableview and I have created 3 different Cell Classes (with their nibs)
I have registered the nib files with the tableview by adding this to the viewdidload method:
[self.tableView registerNib:[UINib nibWithNibName:#"MyCellType1" bundle:nil] forCellReuseIdentifier:#"CellType1"];
[self.tableView registerNib:[UINib nibWithNibName:#"MyCellType2" bundle:nil] forCellReuseIdentifier:#"CellType2"];
[self.tableView registerNib:[UINib nibWithNibName:#"MyCellType3" bundle:nil] forCellReuseIdentifier:#"CellType3"];
then i fully customized my tableview methods:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
switch (section) {
case 0:
return 2;
break;
case 1:
return 1;
case 2:
return [self.myarray count];
default:
break;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0: {
MyCellType1 *cell = (MyCellType1 *) [tableView dequeueReusableCellWithIdentifier:#"CellType1" forIndexPath:indexPath];
//this section always has 2 rows
if (indexPath.row==0) {
cell.lblTitle.text = #"ID";
cell.lblValue.text = [NSString stringWithFormat:#"%i",1277];
} else {
cell.lblTitle.text = #"Date";
cell.lblValue.text = #"02/03/2015";
}
return cell;
}
break;
case 1: {
MyCellType2 *cell = (MyCellType2 *)[tableView dequeueReusableCellWithIdentifier:#"CellType2" forIndexPath:indexPath];
cell.image1.image = [UIImage imageNamed:#"3"];
cell.image2.image = [UIImage imageNamed:#"4"];
return cell;
}
case 2: {
MyCellType3 *cell = (MyCellType3 *)[tableView dequeueReusableCellWithIdentifier:#"CellType3" forIndexPath:indexPath];
cell.lblName.text = #"New Row";
return cell;
}
default:
break;
}
return nil;
}
I am trying to achive a email like select all functionality in uitableview where on same button tap user can checkmark or remove all checkmark and additionally user can also select/deselect rows(on didSelectRowAtIndexPath). I tried to do but its not working properly, here is my code.
- (IBAction)selectAll:(id)sender
{
if(myBoolean)
{
for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
{
for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
{
[[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryNone];
}
}
myBoolean = NO;
[_selectUnselectButton setTitle:#"Select all Friends" forState:UIControlStateNormal];
}
else
{
for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
{
for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
{
[[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryCheckmark];
NSLog(#"%d-%d",s,r);
}
}
myBoolean = YES;
[_selectUnselectButton setTitle:#"Unselect all Friends" forState:UIControlStateNormal];
}
}
-(void)tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView_ cellForRowAtIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
I wrote a sample code that I adapted to your needs.
Basically it is
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *unifiedID = #"aCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:unifiedID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:unifiedID];
}
cell.textLabel.text = [self.states objectAtIndex:indexPath.row];
//if the indexPath was found among the selected ones, set the checkmark on the cell
cell.accessoryType = ([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *state = [self.states objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if([self isRowSelectedOnTableView:tableView atIndexPath:indexPath]){
[self.selectedCells removeObject:indexPath];
[self.selecedStates removeObject:state];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
[self.selectedCells addObject:indexPath];
[self.selecedStates addObject:state];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
NSLog(#"%#", self.selecedStates);
}
-(BOOL)isRowSelectedOnTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath
{
return ([self.selectedCells containsObject:indexPath]) ? YES : NO;
}
- (IBAction)selectAll:(id)sender {
[self.selecedStates removeAllObjects];
[self.selectedCells removeAllObjects];
NSUInteger numberOfSections = [self.tableView numberOfSections];
for (NSUInteger s = 0; s < numberOfSections; ++s) {
NSUInteger numberOfRowsInSection = [self.tableView numberOfRowsInSection:s];
for (NSUInteger r = 0; r < numberOfRowsInSection; ++r) {
NSIndexPath *idxPath = [NSIndexPath indexPathForRow:r inSection:s];
[self.selectedCells addObject:idxPath];
[self.selecedStates addObject:self.states[idxPath.row]];
}
}
[self.tableView reloadData];
}
- (IBAction)deselectAll:(id)sender {
[self.selecedStates removeAllObjects];
[self.selectedCells removeAllObjects];
[self.tableView reloadData];
}
- (IBAction)toggleAll:(id)sender {
if ([self.states count] == [self.selecedStates count]) {
[sender setTitle:#"select all"];
[self deselectAll:sender];
} else {
[sender setTitle:#"deselect all"];
[self selectAll:sender];
}
}
in action:
You are calling
[[self.iTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] setAccessoryType:UITableViewCellAccessoryNone];
for every row in every section within the tableView. if you have many row, this is ver inefficient, as it will deal with rows not on the screen. But this is not needed. just put every selected index path into an array and tell the tableView to reload. This will reload the visible cells and due to the implementation of -tableView:cellForRowAtIndexPath: cells wfor new rows will be correctly re-conigured.
Setting the accessory view needs to happen inside the tableView:cellForRowAtIndexPath: method. When you want to change the accessories from outside, the outside method needs to change the model first to indicate that check marks must be placed in certain cells, and then call reloadData on the UITableView.
One way to store what cells are checked is an array of NSIndexSet objects - one index set per section. In the example below I show code for a single section, but you should get an idea of how to make multiple sections work.
// This variable needs to be declared in a place where your data source can get it
NSMutableIndexSet *selected;
// You need to initialize it in the designated initializer, like this:
selected = [[NSMutableIndexSet alloc] init];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if ([selected containsIndex:indexPath.row]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
} else {
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
// Do the rest of your code
return cell;
}
Now in the code where you want to set rows selected or unselected you just need to call [selected addIndex:rowToSelect] or [selected removeIndex:rowToUnselect], and call your table's reloadData.
Use selectRowAtIndexPath:animated:scrollPosition: to select a row
and deselectRowAtIndexPath:animated: to deselect a row.
For more read the UITableView docs
Try this code instead of your old one
- (IBAction)selectAll:(id)sender
{
if(myBoolean)
{
for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
{
for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
{
[self.iTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] animated:YES scrollPosition:UITableViewScrollPositionNone];
}
}
myBoolean = NO;
[_selectUnselectButton setTitle:#"Select all Friends" forState:UIControlStateNormal];
}
else
{
for (NSInteger s = 0; s < self.iTable.numberOfSections; s++)
{
for (NSInteger r = 0; r < [self.iTable numberOfRowsInSection:s]; r++)
{
[self.iTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]] animated:YES scrollPosition:UITableViewScrollPositionNone];
NSLog(#"%d-%d",s,r);
}
}
myBoolean = YES;
[_selectUnselectButton setTitle:#"Unselect all Friends" forState:UIControlStateNormal];
}
}
-(void)tableView:(UITableView *)tableView_ didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView_ cellForRowAtIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
You can follow these steps to implement this,
1) You should have a mutable array to store indexpaths
2) What you can do is, when you tap Check All or Uncheck All, do add or remove all indexpaths to/from array (which you've), reload table for update changes
3) In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath datasource method, check into array using if([array containsObject:indexPath]), if exist mark it checked or unchecked
4) In - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath delegate method, you should check for already existence of indexpath tapped into array, as you've did in 3rd step, and add or remove indexpaths as per the condition, reload table for update changes
Take another NSMutableArray as SelectedArray
in didSelectRowAtIndexPath row You can Add remove objects from SelectedArray.
You can select a UITableViewCell calling UITableViews selectRowAtIndexPath method:
[yourtable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
animated:NO
scrollPosition:UITableViewScrollPositionTop];
Put for loop for selectedArray to putcheckmark only in selected cells or All cells.
check my accepted answer here : Need to create a select all button for UITableView and add selections to an array in iOS
Xcode 8x
Swift 3x
Select Row
let indexPath = IndexPath(row: 0, section: 0)
mytableView.selectRow(at: indexPath, animated: true, scrollPosition: .bottom)
myTableView.delegate?.tableView!(myTableView, didSelectRowAt: indexPath)
DeSelect Row
let deselectIndexPath = IndexPath(row: 7, section: 0)
myTableView.deselectRow(at: deselectIndexPath, animated: true)
myTableView.delegate?.tableView!(tblView, didDeselectRowAt: indexPath)
you can do something like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self doSomethingWithRowAtIndexPath:indexPath];
}
I want checked more cell with one click(touch).
In this code I try to do what I said bat ... I have some problem
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *riga;
int esci=0;
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSDictionary *itemAtIndex = (NSDictionary *)[listad objectAtIndex:indexPath.row];
riga=[itemAtIndex objectForKey:#"riga"];
for (int i=0; i<[righeS count]; i++) {
if ([righeS objectAtIndex:i] ==riga) {
cell.accessoryType=UITableViewCellAccessoryNone;
//[tableView deselectRowAtIndexPath:indexPath animated:YES];
[righeS removeObjectAtIndex:i];
esci=1;
}
}
if (esci==0) {
[righeS addObject:riga];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
//Check if value of qty of the next row is 0
bool trovato=FALSE;
for (int g =0 ;g <[listad count];g++){
NSDictionary *rigaScan=(NSDictionary*)[listad objectAtIndex:g];
if (trovato==TRUE) {
if ([[rigaScan objectForKey:#"qty"] isEqualToString: #"0"]){
NSString *rig;
rig=[rigaScan objectForKey:#"riga"];
[righeS addObject:rig];
cell.accessoryType=UITableViewCellAccessoryCheckmark;
}else{
break;
}
}
if ([rigaScan objectForKey:#"riga"]==riga){
trovato=TRUE;
}
[rigaScan release];
}
//Fine controllo
}
}
In this event I try when the user touches a cell to see if some value of the next cell is 0.
If the value is 0, I have to check also the next cell and the other value in the for iterate.
When the next value is !=0 I put break.
I believe that the logical code is correct but maybe not the syntax for cocoa.
Try this wonderful post from Cocoa With Love - Multiple row selection and editing in a UITableView