如何根据indexPath获取单元格文本?

发布于 2024-09-02 15:56:27 字数 750 浏览 5 评论 0原文

我有一个包含超过 5 个 UITabBarItems 的 UITabBarController,因此 moreNavigationController 可用。

在我的 UITabBarController Delegate 中,我执行以下操作:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//do some stuff
//...

UITableView *moreView = (UITableView *)self.tabBarController.moreNavigationController.topViewController.view;
    moreView.delegate = self;
}

我想实现一个 UITableViewDelegate,以便我可以捕获所选的行,设置自定义视图属性,然后推送视图控制器:

- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
  //how can I get the text of the cell here?
}

我需要在以下情况下获取单元格的文本:用户点击一行。我怎样才能做到这一点?

I have a UITabBarController with more than 5 UITabBarItems so the moreNavigationController is available.

In my UITabBarController Delegate I do the following:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
//do some stuff
//...

UITableView *moreView = (UITableView *)self.tabBarController.moreNavigationController.topViewController.view;
    moreView.delegate = self;
}

I want to implement a UITableViewDelegate so I can capture the row that was selected, set a custom view property and then push the view controller:

- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
  //how can I get the text of the cell here?
}

I need to get the text of a cell when the user taps on a row. How can I accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇柔作态 2024-09-09 15:56:27
- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
      //how can I get the text of the cell here?
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
      NSString *str = cell.textLabel.text;
}

更好的解决方案是维护单元格数组并直接在此处使用它

    // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

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

    Service *service = [self.nearMeArray objectAtIndex:indexPath.row];
    cell.textLabel.text = service.name;
    cell.detailTextLabel.text = service.description;
    if(![self.mutArray containsObject:cell])
          [self.mutArray insertObject:cell atIndex:indexPath.row];
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [self.mutArray objectAtIndex:indexPath.row];
    NSString *str = cell.textLabel.text;

}
- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
      //how can I get the text of the cell here?
      UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
      NSString *str = cell.textLabel.text;
}

A better Solution is to maintain Array of cell and use it directly here

    // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

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

    Service *service = [self.nearMeArray objectAtIndex:indexPath.row];
    cell.textLabel.text = service.name;
    cell.detailTextLabel.text = service.description;
    if(![self.mutArray containsObject:cell])
          [self.mutArray insertObject:cell atIndex:indexPath.row];
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [self.mutArray objectAtIndex:indexPath.row];
    NSString *str = cell.textLabel.text;

}
堇年纸鸢 2024-09-09 15:56:27

Swift 5

您始终可以根据 IndexPathUITableVIewUICollectionView 获取您的 cell像这样:

tableView.cellForItem(at: indexPath)
collectionView.cellForItem(at: indexPath)

Swift 5

You can always get your cell from UITableVIew or UICollectionView based on IndexPath like this:

tableView.cellForItem(at: indexPath)
collectionView.cellForItem(at: indexPath)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文