iOS如何从tableview单元格中获取按钮文本
Posted
技术标签:
【中文标题】iOS如何从tableview单元格中获取按钮文本【英文标题】:iOS How to get button text from tableview cell 【发布时间】:2015-07-23 05:49:15 【问题描述】:我正在使用简单的表格视图,并且我在每个单元格中添加了按钮,问题是如何从第二个单元格中获取按钮文本,而任何其他单元格编号获取按钮文本我正在使用此代码,但它不起作用
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 中尝试过此代码,但无法正常工作 问候, 尼山特·钱德瓦尼
【问题讨论】:
【参考方案1】:做一件事,为每个按钮赋予标签值,比如
button_chaeckbox.tag=indexpath.row
在 cellForRowAtIndexPath 方法中。
在checkboxAction中这样做 NSString * titleText=sender.titleLabel.text
然后您将检查按钮操作方法来自哪个单元格
喜欢使用条件 if(sender.tag == //你的手机号第二个值//)
我想你只想要这样。
【讨论】:
【参考方案2】:使用此代码重复使用表格视图中的单元格
- (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]
【讨论】:
【参考方案3】:你可以试试这个
在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】:让我指出一件事。如果要遍历表格单元格的所有子视图,则必须将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)
【讨论】:
【参考方案5】:我建议创建一个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
【讨论】:
CustomTableViewCell 和 [cell setCellButton:button_chaeckbox];给我错误我如何设置 你是否创建了一个CustomTableViewCell
类,其属性为CellButton
以上是关于iOS如何从tableview单元格中获取按钮文本的主要内容,如果未能解决你的问题,请参考以下文章
点击保存按钮并存储在字典中时获取表格视图单元格中的文本字段?
如何从单元格中的按钮操作获取 tableview 中的 indexPath?