如何在 UIPopover 中以编程方式创建 UITableView,以便它响应 -cellForRowAtIndexP



我在一个类中有 3 个 UITableViews;其中一个 tableViews 是在 UIPopover 中以编程方式创建的,我在那里给它分配一个标签。 在 -cellForRowAtIndexPath 中,我检查每个表视图的标记,并根据标记 ID 配置表视图。

问题是在调用 -cellForRowAtIndexPath 之前不会创建弹出框。 我不明白如何在弹出窗口中创建表视图的方法中拥有单独的 -cellForRowAtIndexPath。 我在创建tableView的方法中有tableView.dataSource = self。

如何将弹出框中的表视图指向它自己的 -cellForRowAtIndexPath?

所以这并不完全清楚,但似乎你问如何为类中的不同 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;
}

如果您想进一步澄清,或者您对这个主题有进一步的问题,请告诉我。

最新更新