按钮中有一个自定义单元格。通过单击它,它将被选中/取消选中。但是在TableView中单击第一个单元格中的按钮我需要重置其他单元格。我怎么能这样做?
答案 0 :(得分:1)
如果您需要重置所有按钮,则在点击任何表格单元格按钮时,任何时候只能选择一个按钮。然后,您可以在控制器中保持对所选表格单元格的引用并重置它。
如果要重置所有按钮,请点击第一行单元格按钮,然后获取所有可见单元格并重置该单元格。应该从数据源方法返回单元格视图时重置所有其他单元格。
您需要实现委托,以获取任何单元格上按钮点击的通知。
答案 1 :(得分:0)
您可以从UITableView获取所有单元格。从UITableView获取所有单元格后,您可以轻松地从单元格中获取所有子视图并从中重置按钮。
答案 2 :(得分:0)
你可以这样做。
这是代码。这应该写在您的自定义单元格的按钮点击方法中。
E0003
希望这会有所帮助。
答案 3 :(得分:0)
将其放入您的cellforrowatindex方法
if([totalcheckmarkArray containsObject:[NSString stringWithFormat:@"%d",(int)indexPath.row]])
{
[cell.btnCheckbox setImage:[UIImage imageNamed: @"chk_check"] forState:UIControlStateNormal];
}
else
{
[cell.btnCheckbox setImage:[UIImage imageNamed:@"chk_uncheck"]forState:UIControlStateNormal];
}
cell.btnCheckbox.tag=indexPath.row;
[cell.btnCheckbox addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
现在当按钮点击特定位置时,您需要调用以下方法..
#pragma mark CheckBox Actions
-(void)checkBoxClicked:(id)sender{
UIButton *tappedButton = (UIButton*)sender;
int totalSection = (int)self.tableview.numberOfSections;
int totalRow = 0;
if (tappedButton.tag==0) {
[totalcheckmarkArray removeAllObjects];
[indexarray removeAllObjects];
SelectedCateCount=0;
NSLog(@"%d",SelectedCateCount);
}
else {
if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"chk_check"]])
{
SelectedCateCount-=1;
[totalcheckmarkArray removeObject:[NSString stringWithFormat:@"%d",(int)tappedButton.tag]];
[indexarray removeObject:[arrSelectedCategory objectAtIndex:tappedButton.tag]];
}
else
{
SelectedCateCount+=1;
[totalcheckmarkArray addObject:[NSString stringWithFormat:@"%d",(int)tappedButton.tag]];
[indexarray addObject:[arrSelectedCategory objectAtIndex:tappedButton.tag]];
}
NSLog(@"%d",SelectedCateCount);
if (SelectedCateCount==arrSelectedCategory.count-1) {
CellCategory *cell = (CellCategory *)[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[cell.btnCheckbox setImage:[UIImage imageNamed:@"chk_check"] forState:UIControlStateNormal];
[totalcheckmarkArray addObject:[NSString stringWithFormat:@"%d",(int)cell.btnCheckbox.tag]];
}
else{
CellCategory *cell = (CellCategory *)[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[cell.btnCheckbox setImage:[UIImage imageNamed:@"chk_uncheck"] forState:UIControlStateNormal];
[totalcheckmarkArray removeObject:[NSString stringWithFormat:@"%d",(int)cell.btnCheckbox.tag]];
}
}
[_tableview reloadData];
NSLog(@"%@",indexarray);
}
现在根据您的要求更改复选框点击方法
答案 4 :(得分:0)
嗯,使用委托很容易解决您的问题。
创建某处协议:
@protocol CustomCellDelegate <NSObject>
@optional
- (void)handleSelection:(NSIndexPath) indexPath;
@end
在自定义单元格的类中创建属性和func:
@property (nonatomic, weak) id <CustomCellDelegate> delegate;
@property (nonatomic, retain) NSIndexPath *selectedIndexPath;
- (void) handleTap
{
[self.delegate handleSelection:self.selectedIndexPath];
}
然后在你的视图中,控制器的类成为CustomCellDelegate的订户并添加“selected”indexPath的属性:
@property (nonatomic, retain) NSIndexPath *currentlySelectedIP;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// your custom cell stuff
customCell.selected = NO;
customCell.delegate = self;
[customCell.yourButton addTarget:self action:@selector(handleTap) forControlEvents:UIControlEventTouchUpInside];
customCell.selectedIndexPath == indexPath;
if (self.currentlySelectedIP != nil) && (self.currentlySelectedIP == indexPath)
{
customCell.selected = YES; // handle stuff for selected cell/button, whatever
}
}
并处理委托的功能:
- (void) handleSelection:(NSIndexPath) indexPath
{
self.currentlySelectedIP == indexPath;
[self.tableView reloadData];
}