Reputation: 23
In UITableView, I want to put check uncheck buttons,when I click on first row[indexpath.row=0] remaining rows will uncheck and when click on indexpath.row>0 then row=0 unchceck. I have to do viceversa ,
intially I took an array with all objects uncheck and when check it that index will replace object check,
for(int i=0; i<[carwasharray count]; i++)
{
[arrayCheckUnchek addObject:@"Uncheck"];
}
/cell row at indexpath my code is
cell.textLabel.text = [carwasharray objectAtIndex:indexPath.row];
if (indexPath.row==0) {
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setImage:[UIImage imageNamed:@"uncheck_icon.png"] forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(buttonClic:) forControlEvents:UIControlEventTouchUpInside];
button1.tag=1;
[button1 setFrame:CGRectMake(150.0, 7.0, 30.0, 30.0)];
[cell.contentView addSubview:button1];
}
else{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(150.0, 7.0, 30.0, 30.0)];
button.tag=2;
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
[button setImage:[UIImage imageNamed:@"uncheck_icon.png"] forState:UIControlStateNormal];
else
[button setImage:[UIImage imageNamed:@"check_icon"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
}
return cell;
}
-(void)buttonClic:(id)sender
{
//Getting the indexPath of cell of clicked button
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:carwashtable];
NSIndexPath *indexPath = [carwashtable indexPathForRowAtPoint:touchPoint];
UIButton *button = (UIButton *)sender;
if (button1.tag==1)
{
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
{
//[button1 setImage:[UIImage imageNamed:@"check_icon.png"] forState:UIControlStateNormal];
//[button setImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
// [arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Check"];
button.tag=3;
[self buttonClicked:button];
}
else
{
//[button1 setImage:[UIImage imageNamed:@"check_icon.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Check"];
button.tag=3;
[self buttonClicked:button];
}
}
else if(button1.tag==0){
[button1 setImage:[UIImage imageNamed:@"uncheck_icon.png"] forState:UIControlStateNormal];
}
}
-(void)buttonClicked:(id)sender
{
//Getting the indexPath of cell of clicked button
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:carwashtable];
NSIndexPath *indexPath = [carwashtable indexPathForRowAtPoint:touchPoint];
// No need to use tag sender will keep the reference of clicked button
UIButton *button = (UIButton *)sender;
if (button.tag==2) {
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
{
[button setImage:[UIImage imageNamed:@"check_icon.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:@"uncheck_icon.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Check"];
button1.tag=1;
}
else
{
[button setImage:[UIImage imageNamed:@"uncheck_icon.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];
}
}
else
{
if ([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Check"]) {
[button setImage:[UIImage imageNamed:@"uncheck_icon.png"] forState:UIControlStateNormal];
}
[button1 setImage:[UIImage imageNamed:@"check_icon.png"] forState:UIControlStateNormal];
}
now its working only 1st time when I click row=1, the row=0 wil uncheck, upto this ok, but when I recheck row=0 it will also check..
thank in advance
Upvotes: 0
Views: 2558
Reputation: 23
Hi thanks every one who given me a reply.... I got the answer it working fine now
same code but slightly changes...
//cell for row at indexpath....
if (indexPath.row==0) {
[button1 addTarget:self action:@selector(completebuttonClic:) forControlEvents:UIControlEventTouchUpInside];
button1.tag=1;
[button1 setFrame:CGRectMake(240.0, 7.0,25.0, 25.0)];
[cell.contentView addSubview:button1];
}
else{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(240.0, 7.0, 25.0, 25.0)];
button.tag=2;
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
[button setImage:[UIImage imageNamed:@"uncheck_ico.png"] forState:UIControlStateNormal];
else
[button setImage:[UIImage imageNamed:@"check_icon.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
}
return cell;
}
//calling button targets
-(void)completebuttonClic:(id)sender
{
//completebutton clicked
//Getting the indexPath of cell of clicked button
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:carwashtable];
NSIndexPath *indexPath = [carwashtable indexPathForRowAtPoint:touchPoint];
UIButton *button = (UIButton *)sender;
if (button1.tag==1)
{
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
{
button.tag=3;
button1.tag=0;
[self buttonClicked:button];
}
else
{
button.tag=3;
[self buttonClicked:button];
}
}
else if(button1.tag==0){
[button1 setImage:[UIImage imageNamed:@"uncheck_ico.png"] forState:UIControlStateNormal];
[selectrows removeObject:[carwashtypeid objectAtIndex:indexPath.row]];
}
NSLog(@"%@",selectrows);
}
-(void)buttonClicked:(id)sender
{
//Getting the indexPath of cell of clicked button
CGPoint touchPoint = [sender convertPoint:CGPointZero tablviewname];
NSIndexPath *indexPath = [tableviewname indexPathForRowAtPoint:touchPoint];
// No need to use tag sender will keep the reference of clicked button
UIButton *button = (UIButton *)sender;
if (button.tag==2) {
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
{ NSLog(@"%ld",(long)indexPath.row);
[button setImage:[UIImage imageNamed:@"check_icon.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:@"uncheck_ico.png"] forState:UIControlStateNormal];
if (button1.imageView.image==[UIImage imageNamed:@"uncheck_ico.png"]) {
//adding selected rows in a selectrows array
[selectrows removeObject:[carwashtypeid objectAtIndex:0]];
}
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Check"];
[selectrows addObject:[carwashtypeid objectAtIndex:indexPath.row]];
button1.tag=1;
}
else
{
[button setImage:[UIImage imageNamed:@"uncheck_ico.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];
[selectrows removeObject:[carwashtypeid objectAtIndex:indexPath.row]];
}
}
else
{
for (int i=1; i<[carwasharray count]; i++) {
if ([[arrayCheckUnchek objectAtIndex:i] isEqualToString:@"Check"]) {
[arrayCheckUnchek replaceObjectAtIndex:i withObject:@"Uncheck"];
[carwashtable reloadData];
[selectrows removeObject:[carwashtypeid objectAtIndex:i]];
}}
[button1 setImage:[UIImage imageNamed:@"check_icon.png"] forState:UIControlStateNormal];
[selectrows addObject:[carwashtypeid objectAtIndex:indexPath.row]];
}
NSLog(@"%@",selectrows);
Upvotes: 1
Reputation: 4271
This is how I would do it.
I would keep all selected cell's indices in an NSMutableArray
, say selectedCellsArray.
In the didSelectRowAtIndexPath
method I would first check if the selected cell is at index 0 using indexPath.row
. If it is cell 0, I would do this:
[selectedCellsArray removeAllObjects];
[selectedCellsArray addObject:[NSNumber numberWithInt:0]];
If the selected cell's index is not 0, I would check if the selectedCellsArray contains 0, and remove it using:
if([selectedCellsArray containObject:[NSNumber numberWithInt:0]])
[selectedCellsArray removeObject:[NSNumber numberWithInt:0]];
And add the selected cell's index with:
[selectedCellsArray addObject:[NSNumber numberWithInt:indexPath.row]];
So the final code becomes:
if (indexPath.row == 0){
[selectedCellsArray removeAllObjects];
[selectedCellsArray addObject:[NSNumber numberWithInt:0]];
}else{
if([selectedCellsArray containObject:[NSNumber numberWithInt:0]])
[selectedCellsArray removeObject:[NSNumber numberWithInt:0]];
[selectedCellsArray addObject:[NSNumber numberWithInt:indexPath.row]];
}
[yourTableView reloadData];
In cellForRowAtIndexPath:
I would check if the selectedCellsArray contains the current cell. If it does, change it's accessory type with :
if ([selectedCellsArray containObject:[NSNumber numberWithInt:indexPath.row]])
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
else
[cell setAccessoryType:UITableViewCellAccessoryNone];
The advantage here is that you can use the selectedCellsArray to process the information further.
Upvotes: 0
Reputation: 4005
Having an array with string value is not very efficient way to know that current cell checked or unchecked status i would recommend you to create and custom cell and use a custom button class for check button like https://github.com/ardalahmet/SSCheckBoxView so you can ask from cell what is the status
Upvotes: 0
Reputation: 11828
UITableView merely displays the UITableViewCell(s) you want it to.
I would recommend creating a Prototype cell that will hold your text and "CheckBox"
Here is a page that demonstrates a few different approaches to creating custom UITableViewCell objects.
http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/
Understanding how a task can be accomplished is as important as accomplishing the task itself.
Upvotes: 1