【问题标题】:How to programmatically create a UITableView in UIPopover so it responds to -cellForRowAtIndexPath如何以编程方式在 UIPopover 中创建 UITableView 以响应 -cellForRowAtIndexPath
【发布时间】:2015-01-09 12:05:28
【问题描述】:

我在一个类中有 3 个 UITableView;其中一个 tableViews 是在 UIPopover 中以编程方式创建的,我在其中为其分配了一个标签。在 -cellForRowAtIndexPath 中,我检查每个 tableView 的标签,并根据标签 id 配置 tableView。

问题是直到调用 after -cellForRowAtIndexPath 才创建弹出框。我看不到如何在弹出窗口中创建 tableView 的方法中有一个单独的 -cellForRowAtIndexPath 。我在创建 tableView 的方法中有 tableView.dataSource = self。

如何将弹出窗口中的 tableView 指向它自己的 -cellForRowAtIndexPath?

【问题讨论】:

  • 给定的 TableView 将调用包含在由 dataSource 指针标识的类中的 cellForRowAtIndexPath不是 delegate 指针)。不可能有其他的,并且在设置 dataSource 指针(以及适当构造的 TableView 等)之前无法调用 cellForRowAtIndexPath
  • 这是我的错字...我的意思是数据源...(我知道其中的区别)。这就是这个问题被否决的原因吗?
  • 你显然不明白dataSource 是什么。如果没有多个数据源,就不能拥有多个版本的 cellForRowAtIndexPath。你的问题很荒谬。

标签: objective-c uitableview uipopover


【解决方案1】:

所以这并不完全清楚,但您似乎在询问如何为班级中的不同 TableView 分配不同的 cellForRowAtIndexPath。为此,我创建了一小段示例代码来说明如何为单个类中的多个 UITableView 提供不同的数据源集。

如您所见,三个不同的 DataSource 对象可以各自独立控制三个不同 UITableView 中的每一个的cellForRowAtIndexPath。没有理由不能让两个表视图使用一个数据源,然后第三个表使用它自己的。

*注意:没有理由将所有这些都保存在一个文件中,但如果这是您的愿望,您当然可以这样做。

//UITableViewOne
@interface DataSourceOne : NSObject <UITableViewDataSource>
@end

@implementation DataSourceOne
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
    // Setup cell for TableViewOne
}
@end

//UITableViewTwo
@interface DataSourceTwo : NSObject <UITableViewDataSource>
@end

@implementation DataSourceTwo
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
    // Setup cell for TableViewTwo
}
@end

//UITableViewThree
@interface DataSourceThree : NSObject <UITableViewDataSource>
@end

@implementation DataSourceThree
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
    // Setup cell for TableViewThree
}
@end

@interface MultiTableViewController ()

@property (nonatomic,strong) UITableView *tableOne;
@property (nonatomic,strong) UITableView *tableTwo;
@property (nonatomic,strong) UITableView *tableThree;

@property (nonatomic,strong) DataSourceOne *sourceOne;
@property (nonatomic,strong) DataSourceTwo *sourceTwo;
@property (nonatomic,strong) DataSourceThree *sourceThree;

@end

@implementation MultiTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.sourceOne = [DataSourceOne new];
    self.sourceTwo = [DataSourceTwo new];
    self.sourceThree = [DataSourceThree new];

    //Create or Load TableViews from Xib

    self.tableOne.dataSource    = self.sourceOne;
    self.tableTwo.dataSource    = self.sourceTwo;
    self.tableThree.dataSource  = self.sourceThree;

}

如果您需要更多说明,或者您对该主题还有其他问题,请告诉我。

【讨论】:

  • 哇!我不知道该怎么做...非常清楚,今晚晚饭后我会继续...非常感谢!
猜你喜欢
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
  • 2017-03-06
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
相关资源
最近更新 更多