all tableCells in different sections got selected of same row (using NSIndexPath) - iphone

I tried to create a simple checklist style tableView by following:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if ([_selectedRows containsObject:indexPath]) {
[_selectedRows removeObject:indexPath];
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
} else {
[_selectedRows addObject:indexPath];
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
However, when I select the first item and scroll, I found out ALL first items in the same section has the checkMark besides it. Doesn't NSIndexPath contain both row and section? What made all items got selected here? :(
Thanks!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_IDENTIFIER];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL_IDENTIFIER];
}
if ([_selectedRows containsObject: indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
ABRecordRef selectedFriend = CFArrayGetValueAtIndex(_allFriends, [indexPath row]);
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(selectedFriend, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(selectedFriend, kABPersonLastNameProperty));
cell.textLabel.text = [NSString stringWithFormat:#"%# %#", firstName, lastName];
return cell;
}

Because of cell reusage, you also have to remove the checkmark accessory if necessary (in cellForRowAtIndexPath):
if ([_selectedRows containsObject: indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}

You need to set the accessory to UITableViewCellAccessoryNone for the non-selected rows since dequeued rows will keep their accessory.
// put this in your cellForRowAtIndexPath method
if ([_selectedRows containsObject: indexPath]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}

Related

how to code to select multiple row with check mark?

I have taken UITableView. I want to use checkmark on row. When row is selected check mark should show there and when he selects check mark row then check mark should disappear.
I used following code.
In my code only I select multiple row but am able to disappear on same checkmark with selected row.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if (cell == nil) {
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.text=[[gmailList objectAtIndex:indexPath.row]valueForKey:#"Name"];
if(cell.selected)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Configure the cell...
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Uncheck the previous checked row
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.selected)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selected=FALSE;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.selected=TRUE;
}
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath{
if ([tableView tag]==2){
self.clearCheckMark = false;
if ([tableView cellForRowAtIndexPath:indexPath].accessoryType ==UITableViewCellAccessoryCheckmark)
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
else
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
self.index = [tableView indexPathsForSelectedRows];
}
return indexPath;}
Try this- You have to define a global NSMutableDict and use that dict as below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if (cell == nil) {
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.text=[[gmailList objectAtIndex:indexPath.row]valueForKey:#"Name"];
if([[dict objectForKey:[NSString stringWithFormat:"%d",indexPath.row]] isEqualToString:#"YES"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Configure the cell...
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Uncheck the previous checked row
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.selected)
{
cell.accessoryType = UITableViewCellAccessoryNone;
[[dict setObject:#"NO"]] forKey:[NSString stringWithFormat:"%d",indexPath.row];
cell.selected=FALSE;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.selected=TRUE;
[[dict setObject:#"YES"]] forKey:[NSString stringWithFormat:"%d",indexPath.row];
}
}
Set NSMutableDictionary with key like "isChecked" "YES" or "NO" using below method
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
NSMutableDictionary *dic = [gmailList objectAtIndex:indexPath.row];
if([[dic valueForKey:#"isChecked"]boolValue])
{
cell.accessoryType = UITableViewCellAccessoryNone;
[dic setObject:#"NO" forKey:#"isChecked"];
cell.selected=FALSE;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[dic setObject:#"YES" forKey:#"isChecked"];
cell.selected=TRUE;
}
[self.tableView reloadData];
}

Evaluate each row of UITableView separately iPhone

I want to show a picture if each row starts with a certain character, let's say "&" in the below example. The code below seems to only work if the first row doesn't start with '&'. If the first row has '&' all of the rows will have the picture. What's wrong?
- (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 ([[[array objectAtIndex:(indexPath.row)] substringToIndex:1] isEqualToString:#"&"])
{
cell.textLabel.text = [array objectAtIndex:(indexPath.row)];
cell.detailTextLabel.text = [array objectAtIndex:(indexPath.row)+1];
cell.imageView.image = [UIImage imageNamed:#"picture.png"];
return cell;
}
else
{
cell.textLabel.text = [array objectAtIndex:(indexPath.row)];
cell.detailTextLabel.text = [array objectAtIndex:(indexPath.row)+1];
return cell;
}
}
Since the first cell may be reused later on, you have to "reset" its state on every call of cellForRowAtIndexPath. Try adding cell.imageView.image = nil after cell.detailTextLabel.text in the else block.

Add a last cell to UItableview

I have a UITableView whose datasource is a NSMutableArray. The array consists of a set of objects. All the cells are displayed in proper order.
Now I want to know how to display a last cell always with some text alone which is not there in the datasource array.
I hope i am clear enough :)
EDIT :----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// Return the number of rows in the section.
//+1 to add the last extra row
return [appDelegate.list count]+1;
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
NSUInteger index=[indexPath row];
if(index ==([appDelegate.list count]+1)) {
cell.textLabel.text = [NSString stringWithFormat:#"extra cell"];
}else{
Item *i = (Item *) [appDelegate.list objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# (%d %#)",i.iName, i.iQty,i.iUnit];
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
but i get the NSMutableArray out of bounds exception.
What could be wrong ?
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [your_array count] + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
if (row == [your_array count])
{
cell.textLabel.text = [NSString stringWithFormat:#"Some text"];
}
else
{
cell.textLabel.text = your array object text;
}
return cell;
}
Your problem is the value you are checking against index. The list has count objects indexed 0 to count - 1. So you must check against count and not count + 1. As it is now, the request for countth row is entering the else section. There is no object at count in the list array. So you are getting the error. This is the modification.
if(index == [appDelegate.list count] ) {
cell.textLabel.text = [NSString stringWithFormat:#"extra cell"];
}else{
Item *i = (Item *) [appDelegate.list objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# (%d %#)",i.iName, i.iQty,i.iUnit];
}
in numberOfRowInSection return number of rows = your array count +1 then in Cell for cellForRowAtIndexPath check for indexPath.row if it is equla to your array count +1 then create cell that you want to add at last.

Data is mismanaged when I scroll the UITableView

I am using following code,in cellForRowAtIndexPath:(NSIndexPath *)indexPath method
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(currentRow >=[finalArray count]) return cell;
// Configure the cell.
for(int ii=0;ii<[keys count];ii++){
NSString *no22=[dict objectForKey:[keys objectAtIndex:ii]];
no=[NSString stringWithFormat:#"%# %"[keys objectAtIndex:ii],]
}// end of the for loop
if(searching)
cell.text = [copyListOfItems objectAtIndex:indexPath.row];
else {
cell.text=[finalArray objectAtIndex:currentRow];
currentRow=currentRow+1;
}
return cell;
}
I am getting strange behavior when I scroll the table data is completely mismanaged,please any suggestion.
Is because you are reusing cells. You can reuse more type of cells.
And why you are doing if(currentRow >=[finalArray count]) return cell; this?

iphone table view check mark accessory problem

I have a table with 50 rows. I want to select particular rows with checkmark accessory but when i select some rows and scroll down the table then i see pre checked rows. I know that table cell are reused but i want to emit this problem what can i do about this?
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// return [array count];
return 50;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[NSString stringWithFormat:#"%i",[indexPath row]];
return cell;
}
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
You have to set cell.accessoryType in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
The fact is that as you have a reuse identifier, when a new cell is requested, a cell with the identifier is copied. As you have set the checkmark in the latest cell, the checkmask is copied.
You have to store the state of each cell (ie in an array) to recreate the cell with the right value.
Something like that:
BOOL values[50]; //Not the best way, but easy for the example...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[NSString stringWithFormat:#"%i",[indexPath row]];
if (value[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
values[indexPath.row] = !value[indexPath.row];
if (value[indexPath.row]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
You can do it with the help of array ,take one array and each array hold a dict do this condition in array
NSMutableArray *arr = [[NSMutableArray alloc]init];
for(int i=1; i<=50;i++)
{
NSmutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setobject:[nsstring stringwithformat:#"%d",i] valueForkey:#"ID"];
[arr addobject:dict];
[dict release];
}
now rows is [arr count];
and put this check in cell appearance
NSMutabledict *dict1 = [arr objectatindex:indexpath.row];
if([[dict objectforkey:"Check"]==nil])
{
[dict setobject:[nsstring stringwithformate:#"1"] valueforkey:#"Check"];
}else
{
if([[dict objectforkey:"Check"] isequaltostring:#"1"]
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
now put below code in didselect methood
nsmutabledict *dict1 = [arr objectatindex:indexpath.row];
if([[dict objectforkey:"Check"] isequaltostring:#"1"]
{
[dict setobject:[nsstring stringwithformate:#"2"] valueforkey:#"Check"];
}
else
{
[dict setobject:[nsstring stringwithformate:#"1"] valueforkey:#"Check"];
}