Ios 自定义单元格和按钮
按钮中有一个自定义单元格。单击它,它将变为选中/未选中。但是在TableView中,通过单击第一个单元格中的按钮,我需要在其他单元格上重置所选内容。我如何才能做到这一点?您可以从UITableView获取所有单元格。从UITableView获取所有单元格后,您可以轻松地从单元格中获取所有子视图并从中重置按钮。您可以这样做Ios 自定义单元格和按钮,ios,objective-c,iphone,xcode,Ios,Objective C,Iphone,Xcode,按钮中有一个自定义单元格。单击它,它将变为选中/未选中。但是在TableView中,通过单击第一个单元格中的按钮,我需要在其他单元格上重置所选内容。我如何才能做到这一点?您可以从UITableView获取所有单元格。从UITableView获取所有单元格后,您可以轻松地从单元格中获取所有子视图并从中重置按钮。您可以这样做 当用户点击任何按钮时,首先取消选择所有按钮 然后选择您点击的按钮 这是代码。这应该写在自定义单元格的按钮点击方法中 for (UIView *theView in self.s
for (UIView *theView in self.subviews)
{
if ([theView isKindOfClass:[UIView class]])
{
for (UIButton *theButton in theView.subviews)
{
if ([theButton isKindOfClass:[UIButton class]])
{
[theButton setSelected:NO];
}
}
}
}
希望这有帮助 如果您需要重置所有按钮,在点击任何表格单元格按钮时,任何时候都只能选择一个按钮。然后,您可以在控制器中保留对所选表格单元格的引用并将其重置 若要重置所有按钮,请点击第一行单元格按钮,然后获取所有可见单元格并重置该单元格。从datasource方法返回单元格视图时,应重置所有其他单元格
您需要实现委托,以获取任何单元格上的按钮点击通知。将其放入您的CellForRowatinex方法中
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);
}
现在,根据您在复选框单击方法中的需求更改好吧,使用委托很容易解决您的问题 创建一个类似于:
@protocol CustomCellDelegate <NSObject>
@optional
- (void)handleSelection:(NSIndexPath) indexPath;
@end
并处理委托人的功能:
- (void) handleSelection:(NSIndexPath) indexPath
{
self.currentlySelectedIP == indexPath;
[self.tableView reloadData];
}
如何获取所有单元格?NSArray*cells=[tableView visibleCells]您需要tableView最后一个单元格或第一个单元格中的可单击按钮?可单击所有单元格,但当我点击第一个单元格中的按钮时,所选按钮其他单元格将重置
@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];
}