关于静态的 tableView 中嵌套动态的 tableView

2016-07-19 20:41:14 +08:00
 LH0811

静态的 tableView 中的某个 cell 放了另外一个 myTableView (就是一个 tableView )这两个 tableView 的代理都是这个静态的 TableViewController ,当 myTableView 的 section 或者 row 超过了静态单元格的 section 或 row 的时候就崩溃了, 报了 Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]'数组越界错误。
是不是静态 tableView 和动态 tableView 不能同时设置同一个对象作为代理,只能把这个 mytableView 的代理放设置成其他对象吗?

#import "TableViewController.h"

@interface TableViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *mytableview;

@end

@implementation TableViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView == self.tableView) {
        return [super numberOfSectionsInTableView:tableView];//静态 tableView 1
    }
    return 1; //动态的 tableView 如果大于 1 就崩溃
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (tableView == self.tableView) {
        return [super tableView:tableView numberOfRowsInSection:section];//静态 tableView 2
    }
    return 3; // 3>2 崩溃了
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView isEqual:self.tableView]) {
        return [super tableView:tableView cellForRowAtIndexPath:indexPath];
    }else {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"acell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"acell"];
        }
        cell.textLabel.text = [NSString stringWithFormat:@"%ld-%ld",indexPath.row,indexPath.section];
        
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.tableView) {
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
    }
    return 44;
}


3423 次点击
所在节点    iDev
4 条回复
expkzb
2016-07-19 21:00:28 +08:00
你放个 stackview 不行么,没有这么玩的
jiangdaohong
2016-07-20 11:27:03 +08:00
为什么这么搞啊,直接做成两个 section 不可以吗
ma125125t
2016-07-20 15:56:01 +08:00
用 childController 吧...
Tangdixi
2016-07-29 14:02:37 +08:00
tableView 里面嵌套 tableView 想想就觉得蛋疼啊 ......

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/293564

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX