在视图控制器之间传递 UITableViewCell

3
我希望您能在一个视图控制器中选择UITableView的UITableViewCell,并将单元格的数据传递到另一个视图控制器。
代码:
-(void)pushView
{
myView.mainCell = [self.tableView cellForRowAtIndexPath:[NSIndexPath     indexPathWithIndex:currentCell]];
[self.navigationController pushViewController:myView animated:YES];
}

myView 是我想要从第一个视图push的视图。 mainCellmyView 的 UITableViewCell 属性。我希望它与所选单元格完全一致。 currentCell 仅是返回所选单元格的行号的整数。

我如何在视图控制器之间传递一个单元格?


你的单元格不应该包含任何要传递的数据。所有数据都应该保存在UITableViewController中。然后,它只是填充单元格并显示数据。如果要将数据传递到其他地方,只需从UITableViewController中获取数据并传递即可。无论是NSArray、NSString、NSDictionary还是自定义类。 - Fogmeister
所以,如果我想在其他视图控制器中看到相同的单元格,我需要使用与原始单元格相同的信息重新创建它?没有一种方法可以直接传递整个单元格吗? - user2480176
@user2480176 这将比传递用于创建单元格的数据并在之后重新创建它要复杂得多:在下一个 VC 中,您将需要使用 UITableView 的协议来填充您的 tableview,这将无论如何都需要原始数据。 - Maen
1
不,那会非常混乱。工作流程应该是这样的:您的数据模型向第一个表提供数据。也许模型驻留在第一个视图控制器中,也可能是另一个对象。当用户从表中选择单元格时,您的数据模型应将填充单元格的数据转发到下一个视图控制器,该视图控制器将该数据合并到其表中。传递单元格对象会在内存管理方面变得混乱,并且总体上都很糟糕。 - Daddy
好的,我明白你想要什么。我正在写答案。 - Fogmeister
2个回答

2

实际上,您不需要传递单元格,因为这会破坏引用,正如许多人所评论的那样。请看这篇文章。它讨论了您正在面对的相同问题。

- (IBAction)nextScreenButtonTapped:(id)sender
{
DestinationViewController *destController = [[DestinationViewController alloc] init];
//pass the data here
destController.data = [SourceControllerDataSource ObjectAtIndex:currentCell];    

[self.navigationController pushViewController:destController animated:YES];
}

1
啊,我现在明白你想要什么了。
你想在表格视图单元格中显示一些数据。然后移动到应用程序的其他位置,并以完全相同的方式在不同的表格视图中显示相同的数据。
然后你需要做的是...
首先创建一个新类,它是UITableViewCell的子类,将其命名为类似MyTableViewCell的东西。
接下来的部分取决于您是否使用Interface Builder,但是我现在将所有内容都在代码中完成。
在新类中,在.h文件中创建您的接口属性。
@interface MyTableViewCell : UITableViewCell

@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *someImageView;
etc...

@end

现在在.m文件中,您可以这样设置...
@implementation MyTableViewCell
- (void)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //set up your labels and add to the contentView.
        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        [self.contentView addSubView:self.nameLabel];

        self.someImageView = ...
        [self.contentView addSubView:self.someImageView];

        // and so on for all your interface stuff.
    }
    return self;
}

@end

现在,在您想要使用此单元格的UITableViewController中,您可以执行以下操作...
- (void)viewDidLoad
{
    // other stuff

    [self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"MyCustomCellReuseIdentifier"];

    // other stuff
}

然后在行的单元格中...

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *customCell = [tableView dequeueReusableCellWithIdentifier:@"MyCustomCellReuseIdentifier"];

    customCell.nameLabel.text = //some string that you got from the data
    customCell.someImageView.image = //some image that you got from the data

    return customCell;
}

通过这样做,您可以在多个位置使用相同的单元格布局,并且您需要做的就是填充数据。
当您将数据传递给新的表视图时,您可以使用相同的单元格类来重新填充它所传递的数据。
永远不要传递UIView或UIView子类。它们不应该以这种方式包含数据。它们仅用于显示数据。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接