在UITableView中,我想要放置check取消选中按钮,当我单击第一个rowindexpath.row=0时,剩下的行将取消选中,当单击indexpath.row>0,然后单击row=0 uncheck。我得反之亦然,
在错误的情况下,我使用了一个数组,所有对象都取消检查,当检查它时,索引将取代对象检查,
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];
}现在它只工作第一次,当我点击row=1,row=0将取消检查,直到这个ok,但是当我再次检查row=0时,它也会检查。
提前感谢
发布于 2014-01-13 06:25:06
嗨,谢谢每一个给我答复的人.我知道答案了,现在一切都好了
相同的代码但略有变化..。
//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);发布于 2014-01-08 06:33:07
UITableView只是显示您希望它的UITableViewCell。
我建议创建一个包含文本和"CheckBox“的原型单元格。
下面的页面演示了创建自定义UITableViewCell对象的几种不同方法。
http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/
理解如何完成一项任务和完成任务本身一样重要。
发布于 2014-01-08 06:50:05
拥有一个具有字符串值的数组并不是知道当前单元格已选中或未选中状态的非常有效的方法,我建议您创建和自定义单元格,并为https://github.com/ardalahmet/SSCheckBoxView之类的check按钮使用自定义按钮类,这样您就可以从单元格中询问状态是什么。
https://stackoverflow.com/questions/20988200
复制相似问题