【问题标题】:How to get the value of cell.textlabel.text in my prepareForSegue method?如何在我的 prepareForSegue 方法中获取 cell.textlabel.text 的值?
【发布时间】:2014-02-06 23:08:28
【问题描述】:

所以我在 DetailViewController 的 .h 中创建了一个名为 cellNumString 的字符串。在我的 HistoryTableViewController.m 中,我有一个 prepareForSegue 方法,在该方法中我将字符串设置为任何内容。现在我有这个:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"Push"]) {

        HistoryDetailsViewController *detailVC = (HistoryDetailsViewController*)segue.destinationViewController;
        [detailVC setCellNumString:@" cell number"];
    }
}

我想要的只是将@“单元格编号”部分更改为 cell.textlabel.text。但我不能使用手机。如何获取 cell.textlabel.text 的值并将其用作传递给下一个视图的字符串?

感谢所有帮助,谢谢。

【问题讨论】:

  • 在调用 performSegue 时将单元格作为发件人传递

标签: ios objective-c uitableview uistoryboardsegue


【解决方案1】:

如果segue在单元格上,你可以使用这个答案https://stackoverflow.com/a/13989915/1835155

您还可以使用以下方法获取所选单元格:

NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedIndexPath];
NSLog(@"%@", cell.textLabel.text);

【讨论】:

  • 谢谢谢谢谢谢谢谢!!!!你不知道我为了让这一切正常工作而努力了多久!我可以在 1 分钟内接受你的回答!
  • 等等,我还有一个简短的问题。我能够获得 cell.textlabel.text 但现在我想知道如何在 prepareforSegue 方法中获得两个不同的部分?
  • 啊,不管它只是 if (indexPath.section == 0) { }
【解决方案2】:

假设您要将字符串设置为选定的单元格,或者您知道要访问其文本标签的单元格,您可以执行以下操作:

//If you want the selected cell    
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
//If you know the cell row index e.g. 1, 2, 3...
NSIndexPath *path = [NSIndexPath indexPathWithIndex:yourRow];
//Create the cell at the index path
UITableViewCell *cell = [tableView cellForRowAtIndexPath:path]
//Do stuff with cell...

【讨论】:

    【解决方案3】:

    将选定的单元格作为发件人参数传递给performSegueWithIdentifier:sender:,如下所示:

    [self performSegueWithIdentifier:@"showPromoViewController" sender:cell];
    

    然后在prepareForSegue:sender:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
        if ([[segue identifier] isEqualToString:@"Push"]) {
    
            HistoryDetailsViewController *detailVC = (HistoryDetailsViewController*)segue.destinationViewController;
    
            // Get the value of cell.textlabel.text
            UITableViewCell *cell = (UITableViewCell *)sender;
            NSString *text = cell.textLabel.text;
    
            // Pass it to the destination view controller
            [detailVC setCellNumString:text];
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-11
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-10
      相关资源
      最近更新 更多