Ios 如何在单元子视图中查找按钮
你好! 这里有一个问题:我以编程方式在UITableView的单元格中添加了一个按钮,如下所示:Ios 如何在单元子视图中查找按钮,ios,uitableview,uibutton,Ios,Uitableview,Uibutton,你好! 这里有一个问题:我以编程方式在UITableView的单元格中添加了一个按钮,如下所示: UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame]; [button setImage:[UIImage imageNamed:@"Button_off_weather"] forState:UIControlStateNormal]; button.tag = indexPath.row; [button addT
UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
[button setImage:[UIImage imageNamed:@"Button_off_weather"] forState:UIControlStateNormal];
button.tag = indexPath.row;
[button addTarget:self action:@selector(buttonCityTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview: button];
NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [_tableView numberOfSections]; ++j)
{
for (NSInteger i = 0; i < [_tableView numberOfRowsInSection:j]; ++i)
{
[cells addObject:[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
for (UITableViewCell *cell in cells)
{
for (id theView in cell.subviews) {
if ([theView isKindOfClass:[UIButton class]]) {
[theView performSelector:@selector(buttonOff:)];
}
}
}
}
}
它很好用。
然后我想在表中找到它并按如下方式关闭:
UIButton *button = [[UIButton alloc] initWithFrame:buttonFrame];
[button setImage:[UIImage imageNamed:@"Button_off_weather"] forState:UIControlStateNormal];
button.tag = indexPath.row;
[button addTarget:self action:@selector(buttonCityTapped:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview: button];
NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [_tableView numberOfSections]; ++j)
{
for (NSInteger i = 0; i < [_tableView numberOfRowsInSection:j]; ++i)
{
[cells addObject:[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
for (UITableViewCell *cell in cells)
{
for (id theView in cell.subviews) {
if ([theView isKindOfClass:[UIButton class]]) {
[theView performSelector:@selector(buttonOff:)];
}
}
}
}
}
NSMutableArray*单元格=[[NSMutableArray alloc]init];
对于(NSInteger j=0;j<[_tableviewnumberofsections];++j)
{
对于(NSInteger i=0;i<[\u表视图行数部分:j];++i)
{
[cells addObject:[\u tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i第1节:j]];
用于(UITableViewCell*单元格中的单元格)
{
for(单元格中的视图id.子视图){
if([视图是类的一种:[UIButton类]]){
[查看性能选择器:@selector(按钮关闭:)];
}
}
}
}
}
这是行不通的。有什么想法吗?使用
@属性创建一个自定义单元格子类,使按钮可用。这是正确的做法
您当前的方法在当前和将来都很容易出错。尽量不要依赖遍历视图层次结构的代码,特别是当您无法控制视图层次结构的某些部分(如单元子视图)时。创建一个自定义单元子类,并使用@属性
使按钮可用。这是正确的做法
您当前的方法在当前和将来都很容易出错。尽量不要依赖遍历视图层次结构的代码,特别是如果您无法控制视图层次结构的某些部分(如单元子视图)。检查此项:对我来说很好
int sections = [self.tableView numberOfSections];
for (int i=0; i<sections; i++) {
//For the Custom cell
NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:i] ;
CellStar *cell =(CellStar *) [self.tableView cellForRowAtIndexPath:myIP];
UITextView *txtVi ;
NSArray *subviews = cell.contentView.subviews;
for(id element in subviews) {
if ([element isKindOfClass:[UITextView class]]){
NSLog(@"element is a UItextView\n");
txtVi = (UITextView *)element;
}
if([element isKindOfClass:[UIButton class]]){
UIButton *btn = (UIButton *) element;
NSLog(@"element is UIButton\n");
}
}
//[self.tableView reloadData];
}
int sections=[self.tableView numberOfSections];
对于(inti=0;i检查:对我来说很好
int sections = [self.tableView numberOfSections];
for (int i=0; i<sections; i++) {
//For the Custom cell
NSIndexPath *myIP = [NSIndexPath indexPathForRow:0 inSection:i] ;
CellStar *cell =(CellStar *) [self.tableView cellForRowAtIndexPath:myIP];
UITextView *txtVi ;
NSArray *subviews = cell.contentView.subviews;
for(id element in subviews) {
if ([element isKindOfClass:[UITextView class]]){
NSLog(@"element is a UItextView\n");
txtVi = (UITextView *)element;
}
if([element isKindOfClass:[UIButton class]]){
UIButton *btn = (UIButton *) element;
NSLog(@"element is UIButton\n");
}
}
//[self.tableView reloadData];
}
int sections=[self.tableView numberOfSections];
对于(int i=0;i将枚举与[cell.contentView子视图]
for(UIView *view in [cell.contentView subviews])
{
if([view isKindOfClass:[UIButton class]])
{
[theView performSelector:@selector(buttonOff:)];
}
}
将枚举与[cell.contentView子视图]
for(UIView *view in [cell.contentView subviews])
{
if([view isKindOfClass:[UIButton class]])
{
[theView performSelector:@selector(buttonOff:)];
}
}
请清楚地分享你的问题。请清楚地分享你的问题。谢谢你的建议!我会照你说的做。谢谢你的建议!我会照你说的做。