我正在使用简单的tableview,并且我在每个单元格中都添加了按钮,问题是如何从单元格编号第二个获取按钮文本以及任何其他单元格编号获取按钮文本我正在使用此代码但它无法正常工作
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
UITableViewCell *cell = [tbl_view cellForRowAtIndexPath:indexPath];
for( UITableViewCell *getview in cell.subviews)
{
if([getview isKindOfClass:[UIButton class]])
{
NSLog(@"sdfsdfsd");
// UIButton *button = (UIButton *);
}
}
此代码是tableview单元格中的设置按钮
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 5, 250,20)];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell=[[UITableViewCell alloc] init];
}
cell.backgroundColor=[UIColor clearColor];
UIButton *button_chaeckbox = [UIButton buttonWithType:UIButtonTypeCustom];
button_chaeckbox.backgroundColor=[UIColor clearColor];
button_chaeckbox.frame = CGRectMake(10, 10, 15, 15);
if( [checkedArray containsObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]])
{
[button_chaeckbox setBackgroundImage:[UIImage imageNamed:@"checked_checkbox.png"] forState:UIControlStateNormal];
}else
{
[button_chaeckbox setBackgroundImage:[UIImage imageNamed:@"empty_box_b.png"] forState:UIControlStateNormal];
}
[button_chaeckbox setTitle:@"creaButtonname" forState:UIControlStateNormal];
button_chaeckbox.tag=indexPath.row;
[button_chaeckbox addTarget:self
action:@selector(checkboxAction:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button_chaeckbox];
}
请给我解决方案,我在ios 7和ios 8中尝试了这个代码,但这不起作用 问候, Nishant Chandwani
答案 0 :(得分:1)
有一件事给每个按钮标记值,如
button_chaeckbox.tag = indexpath.row
in cellForRowAtIndexPath Method。
在checkboxAction中的是这样的 NSString * titleText = sender.titleLabel.text
您将检查按钮操作方法,从哪个单元格
喜欢使用条件if(sender.tag == //你的手机号码第二个值//)
我想你只想这样。
答案 1 :(得分:1)
在表格视图中使用此代码重复使用单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell=[[UITableViewCell alloc] init];
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 5, 250,20)];
cell.backgroundColor=[UIColor clearColor];
UIButton *button_chaeckbox = [UIButton buttonWithType:UIButtonTypeCustom];
button_chaeckbox.backgroundColor=[UIColor clearColor];
button_chaeckbox.frame = CGRectMake(10, 10, 15, 15);
[button_chaeckbox addTarget:self
action:@selector(checkboxAction:)
forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button_chaeckbox];
button_chaeckbox.tag=1001;
}
if( [checkedArray containsObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]])
{
[button_chaeckbox setBackgroundImage:[UIImage imageNamed:@"checked_checkbox.png"] forState:UIControlStateNormal];
}else
{
[button_chaeckbox setBackgroundImage:[UIImage imageNamed:@"empty_box_b.png"] forState:UIControlStateNormal];
}
}
使用此代码从单元格中获取按钮
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
UITableViewCell *cell = [tbl_view cellForRowAtIndexPath:indexPath];
UIButton *button_chaeckbox=(UIButton*)[cell viewWithtag:1001]
答案 2 :(得分:0)
我建议创建一个UITableViewCell
子类,并使该按钮成为该子类的属性。然后在你的代码中看起来像这样。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 5, 250,20)];
if (cell == nil) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.backgroundColor=[UIColor clearColor];
UIButton *button_chaeckbox = [UIButton buttonWithType:UIButtonTypeCustom];
button_chaeckbox.backgroundColor=[UIColor clearColor];
button_chaeckbox.frame = CGRectMake(10, 10, 15, 15);
if( [checkedArray containsObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]])
{
[button_chaeckbox setBackgroundImage:[UIImage imageNamed:@"checked_checkbox.png"] forState:UIControlStateNormal];
} else {
[button_chaeckbox setBackgroundImage:[UIImage imageNamed:@"empty_box_b.png"] forState:UIControlStateNormal];
}
[button_chaeckbox addTarget:self
action:@selector(checkboxAction:)
forControlEvents:UIControlEventTouchUpInside];
[cell setCellButton:button_chaeckbox];
return cell;
}
然后按下按钮:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
CustomTableCell *cell = (CustomTableCell *)[tbl_view cellForRowAtIndexPath:indexPath];
UIButton * yourButton = cell.CellButton
答案 3 :(得分:0)
您可以尝试
cellForRowAtIndexPath
[cell.button setTitle:@"RAM" forState:UIControlStateNormal];
cell.button.tag=indexPath.row;
[cell.button addTarget:self action:@selector(cellBtn:) forControlEvents:UIControlEventTouchUpInside];
选择方法
-(void)cellBtn :(UIButton *)sender
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
AlertCustomCell *cell = [tableview cellForRowAtIndexPath:indexPath];
NSString *user = cell.button.titleLabel.text;
NSLog(@"Text of button is :%@",user);
}
我希望它会起作用
答案 4 :(得分:0)
让我指出一件事。如果你想遍历表格单元格的所有子视图,你必须for( UITableViewCell *getview in cell.subviews)
到for( UIView *getview in cell.subviews)
代码看起来像这样
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
UITableViewCell *cell = [tbl_view cellForRowAtIndexPath:indexPath];
for( UIView *getview in cell.subviews)
{
if([getview isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton *)getView;
NSLog @("%@",button.titleLabel.text)
}
}