Reputation: 979
I am new in iOS and I am facing problem regarding to select and deselect the table view cell. For deselect I used didDeSelectRowAtIndexPath but it not get call
My code is like this
- (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];
}
cell.textLabel.text=[NSString stringWithFormat:@"%@",[reportshortActivityarray objectAtIndex:indexPath.row]];
if([Selectedarray containsObject:[reportshortActivityarray objectAtIndex:indexPath.row]])
{
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
else
{
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [activitytable cellForRowAtIndexPath:indexPath];
if(![Selectedarray containsObject:[reportshortActivityarray objectAtIndex:indexPath.row]]){
[Selectedarray addObject:[reportshortActivityarray objectAtIndex:indexPath.row]];
txtactivity.text = [Selectedarray componentsJoinedByString:@","];
DefaultActivityString=txtactivity.text;
}
NSLog(@"Selected Value =%@",txtactivity.text);
if(![SelectedIDarray containsObject:[reportidActivityarray objectAtIndex:indexPath.row]]){
[SelectedIDarray addObject:[reportidActivityarray objectAtIndex:indexPath.row]];
lblactivity.text = [SelectedIDarray componentsJoinedByString:@","];
}
NSLog(@"Selected Value =%@",lblactivity.text);
if (cell.selected) {
// ... Uncheck
[activitytable deselectRowAtIndexPath:indexPath animated:YES];
}
[[activitytable cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];
activitytable.hidden=NO;
}
- (void)tableView:(UITableView *)tableView didDeSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if([Selectedarray containsObject:[reportshortActivityarray objectAtIndex:indexPath.row]]){
[Selectedarray removeObject:[reportshortActivityarray objectAtIndex:indexPath.row]];
txtactivity.text = [Selectedarray componentsJoinedByString:@","];
}
if([SelectedIDarray containsObject:[reportidActivityarray objectAtIndex:indexPath.row]]){
[SelectedIDarray removeObject:[reportidActivityarray objectAtIndex:indexPath.row]];
lblactivity.text = [SelectedIDarray componentsJoinedByString:@","];
}
NSLog(@"Selected Value =%@",lblactivity.text);
[[activitytable cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];
activitytable.hidden=NO;
}
I am using this code for selecting and deselecting it selecting properly but not deselect.
As in the image I am able to select the row but not able to deselect it.
Upvotes: 1
Views: 564
Reputation: 267
I checked your code, I think you want to make a menu, when click item of menu, call the didselectrow method and change the cell's accessorytype to mark, when second time click the same or different item,change accessorytype to none.
However you click item first or second time, if you want to change the accessorytype of cell, make a property BOOL mark for model in datasource array to store the menu item's status of select.
In the didselectrow
method, change model's mark, then reloaddata. You don't need any code in diddeselectrow
method. You had some mistakes on understanding the method diddeselectrow
.
Upvotes: 0
Reputation: 531
If I recall correctly there is a counter-intuitive mechanism for that: Unless you enable allowsMultipleSelection property of TableView, that protocol method won't be fired up. Give it a shot if you still haven't solved it.
Upvotes: 1
Reputation: 285072
It's highly recommended to use code completion. That will give you the right spelling (case does matter!):
...didDeselect...
Note the lowercase select
Upvotes: 3
Reputation: 168
Just write in method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
...
[tableView deselectRowAtIndexPath:indexPath animated:YES];
...
}
Upvotes: -2