iphone - 如何从 uitableview 中的选定行填充标签字段

标签 iphone sqlite uitableview

我有一个从 sqlite 查询填充的 uitableview。

我想选择或单击一行,然后在 uilabel 字段中显示该行的值。向用户显示该行已被选中。
我还想将该值传递给稍后将调用的不同 Controller 。

这是我的 cellForRowAtIndexPath 的副本:

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

    static NSString *CellIdentifier = @"psystem";
    PSystem *psystem = [self.ppdm_systems objectAtIndex:indexPath.row];

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

    // Set up the cell...
    //  self.accessoryType = UITableViewCellAccessoryCheckmark;
    cell.textLabel.text = psystem.system_id; 
    return cell;
}

我在各种实验中取出了 _label.text ....。

现在不起作用的是将值传递给不同的 Controller 。

使用此处列出的示例,源 Controller 是 TableViewController 并且是设置值的位置。目标 Controller 是 DetailViewController。
我可以传入标签栏的标题,但那是来自 TableView --> DetailView。
我不确定如何从 tableview 中提取;即:Tableview <-- 当我在 DetailView 中时的 DetailView。

谢谢

最佳答案

在您的 UIViewController , 实现:

- (MyObject *)valueForSelectedRow {
    MyCell *cell = (MyCell *)[self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]];
    return cell.myObject;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Get value
    MyObject *object = [self valueForSelectedRow];

    // Update the label, assumed that _label is a pointer to a UILabel view object.
    _label.text = object.myValue;
}

当你想要推送一个新的 View Controller 时,你只需调用 -valueForSelectedRow然后使用该值来插入 Controller 。

这假设您有一个 UITableViewCell子类,其属性设置为某个模型对象。如果你没有那个,只需设置 text属性(property),NSString对象将是您的“模型”对象,尽管当您的单元处理自定义模型对象时会更容易。

编辑:感谢您编辑您的答案。我现在有了我需要的信息。在这一行:cell.textLabel.text = psystem.system_id ,您只需设置 textLabel 即可设置单元格的文本属性。这就是我在上一段中所描述的。我总是创建一个 UITableViewCell子类,用一个属性设置完整的PSystem目的。当您分配 PSystem对象到单元格,它将处理它的内容,因此您可以轻松地在 View 中管理 View 。这是一种非常强制的方法,因为您不必再​​次查看 Controller 来更改 View 的内容。

但是,它可以按照您目前拥有的方式完成。它看起来像:
- (NSString *)valueForSelectedRow {
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[self.tableView indexPathForSelectedRow]];
    return cell.textLabel.text;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Get value
    NSString *value = [self valueForSelectedRow];

    // Update the label, assumed that _label is a pointer to a UILabel view object.
    _label.text = value;
}

在这种情况下,您的 PSystem模型已替换为 NSString目的。为此,这就足够了,但拥有对象本身可能会容易得多。好的,这也可以通过选择 PSystem 来完成再次来自 p_system 的对象NSIndexPath 的数组,但是一旦你想出更复杂的表格 View ,事情就会变得更加困难。

关于iphone - 如何从 uitableview 中的选定行填充标签字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1935788/

相关文章:

iphone - 在 OpenGL ES (iPhone) 中绘制到离屏渲染缓冲区

iphone - 在 iOS 上保存 ID3 标签

iphone - 使用 CoreAnimation 在 iOS 中实现流畅的动画

iphone - Google Map API V3是否包含加密

sql - 部署使用 SQLite 的 Windows 8 Metro 应用程序

ios - 编辑栏按钮无法按预期工作

ios - 如何每 x 秒刷新 UITableView 的一个随机 UITableViewCell x 次。 Objective-C

java - 我无法从 sqlite 解析日期。解析异常 : Unparseable date

android - 如何将时间戳(长毫秒)值与android中的sqlite数据库进行比较

IOS 下拉与 UITextField 和 UITableView