【问题标题】:UITableViewController in UIViewController as a partial viewUIViewController 中的 UITableViewController 作为局部视图
【发布时间】:2014-07-30 20:44:31
【问题描述】:

我是 ios 开发新手,所以我的问题可能很简单! 我想在不同的地方使用 UITableViewController(可重用),所以作为 .net 开发人员,我想创建一个 UITableViewController 并将其加载到不同的视图中(如 MVC.net 中的部分视图) 我知道想要包含此表的 UIView 应该实现 UITableViewDelegate 和 UITableViewDataSource 方法,但我不想这样做,我的意思是我希望 Partial 视图处理所有这些逻辑,因为这是可以访问的视图核心数据。

我已经搜索了不同的解决方案,在几乎所有解决方案中,我都必须在每个想要使用该局部视图的单个视图中实现这些方法。

有什么建议吗? 谢谢

【问题讨论】:

  • 您必须实现 UITableViewDelegate 和 UITableViewDataSource 方法才能部分或完全使用 tableview。休息很简单,只需以编程方式创建一个 tableview 并根据需要将其添加到带有框架的视图中。

标签: iphone objective-c uitableview uiviewcontroller xcode5


【解决方案1】:

是的,您可以处理部分视图中的逻辑,例如您的 exTableviewController。 在您的 exTableViewController 中处理协议 UITableViewDataSource 和 UITableViewDelegate,如下所示。

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Test"];
    if (cell==nil) {
        cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Test"];
        //                    cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%i", indexPath.row];
    return cell;
}

并将局部视图和您的视图控制器关联如下。

@interface YourViewController ()
@property (nonatomic, strong) exTableViewController *tableVC ;

@end

@implementation YourViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableVC = [[exTableViewController alloc] init];
    [self.view addSubview:_tableVC.view];
    // Do any additional setup after loading the view, typically from a nib.
}
@end

您还可以使用 YourViewController.xib 自定义子视图的大小。 并将 self.tableVC 与 xib 文件中的子视图绑定。 希望对您有所帮助。

【讨论】:

  • 感谢代码,但是它不起作用,我的意思是没有调用局部视图的viewDidLoad。
  • 您的意思是没有调用 SPTableViewController -(id)init 吗?我将代码从 SPTableViewController 编辑到 exTableViewController 以使其更易于理解。
  • 为避免误解,本例中exTableViewController为局部视图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-14
  • 1970-01-01
  • 2016-08-30
  • 2012-10-02
  • 2013-01-03
  • 1970-01-01
  • 2011-06-01
相关资源
最近更新 更多