【问题标题】:How to get UITableview cell.textlabel.text value from button click?如何从按钮单击中获取 UITableview cell.textlabel.text 值?
【发布时间】:2011-09-16 19:01:55
【问题描述】:

我是 iphone 开发的新手。

我正在做一个有 UItableview 的项目,我在每一行中放置了一个按钮。 现在我的 UItableView 有一个 cell.textlabel、detailLabel 和一个以编程方式放置的按钮。

在 cell.textlabel 中,我有一些来自 json 的值(每个标签都有不同的值),并且在每一行的右上角,我放置了一个按钮。

现在我只想要按钮被按下的那一行的 cell.textlable.text 值。

我为每个按钮获得相同的值。 那么,我的问题是,如何获取按下按钮的特定行的 labeltext 值?

【问题讨论】:

标签: iphone


【解决方案1】:

在按钮创建过程中附加一个标签,例如button1.tag = 9999; 在您的按钮单击操作中,将按钮作为发件人获取

-(void)buttonaction:(UIButton*)sender
{
UITableViewCell * selectedCell = nil;
for(UITableViewCell *cell in [tableView visibleCells])
{
  if([cell viewWithTag:9999] == sender)
  {
     selectedCell = cell;
     break;
  }
}
//do whatever you like with selected cell now.
}

希望对你有帮助!!!

【讨论】:

  • 我需要的是:单击特定行上的按钮如何返回该行的 cell.textlabel 值的值。即当我单击第 1 行的按钮时。 3,它应该只返回该行的文本标签值。
【解决方案2】:

这样解决:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:  (NSIndexPath *)indexPath {



static NSString *CellIdentifier = @"Cell";

cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

    UIButton *tableCellButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain];
    tableCellButton.frame = CGRectMake(200, 1, 80, 20);
    [tableCellButton setTitle:@"showing" forState:UIControlStateNormal];
    [tableCellButton addTarget:self action:@selector(shortcutBottonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:tableCellButton];



}   NSDictionary *listing = [ self.temp objectAtIndex:indexPath.row];
cell.textLabel.text = [listing  objectForKey:@"0"];




return cell;

}

-(IBAction) shortcutBottonClicked:(id)sender{

  //this code solved the problem
 indexPath = [self.tableView indexPathForCell:(UITableViewCell*)[sender superview]];

mlsid2 = [self.tableView cellForRowAtIndexPath:indexPath].textLabel.text;
}

【讨论】:

  • [sender superview] 在通过 IB(在 xib 中)将按钮添加到单元格时实际上不起作用。然后该方法将不会返回 UITableViewCell,而是返回 UITableViewCellContentView(至少在 iOS 6.0 上)
【解决方案3】:

我认为您的意思是表格中的每一行都有一个按钮,并且可以按下任意数量的按钮。用户完成按行按钮后,您需要获取已按的数据行。

如果我正确地阅读了您的内容,那么除非您的表是静态的,否则它将按以下方式工作:

你需要有一个像我假设你的 UITableview 数据源那样的数据结构。例如,一个充满 NSNumber 对象的 NSArray,每个对象对应一个数据行。当按下一行中的按钮时,对应的 NSNumber 设置为 1。当您需要它时,只需遍历 NSNumbers 的 NSArray 以了解您的表数据源中按下按钮的对应行。

需要注意的一点是,您在 UI 中看到的 UItableview 单元格通常会在用户滚动时重复使用 - 至少如果您的表格有可变数量的数据和行(而不是static),因此您不能依赖 UI 元素(例如按钮)来记住状态

【讨论】:

    【解决方案4】:

    在第一个视图 .h 文件中

    @
    
    class viewController;
    #import <UIKit/UIKit.h>
    
    
    @interface firstViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource> {
    
        NSString *_cellTitle;
    
    
    }
    
    @end
    

    在 didSelectRowAtIndex 方法中首先查看 .m 文件添加此代码,如

    viewController *detailViewController = [[viewController alloc]     initWithNibName:@"viewController" bundle:nil];       
    
    NSInteger row = [indexPath row];
    _rowTitle = [_detailList objectAtIndex:row];
    
    detailViewController._barTitle = _rowTitle ;
    

    或者您可以在 buttonPressed 方法中执行此操作

    然后在第二个视图 .h 部分 -

    @interface viewController : UIViewController {
       NSString *_barTitle;
    }
    
    @property (nonatomic, retain) NSString *_barTitle;
    
    @end
    

    在实现部分综合这个属性 在 .m 和 viewDidLoad 方法下

    self.navigationItem.title = _barTitle;
    

    别忘了导入第二个视图.h文件

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-04
      • 2021-08-25
      相关资源
      最近更新 更多