V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
LH0811
V2EX  ›  iDev

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

  •  
  •   LH0811 · 2016-07-19 20:41:14 +08:00 · 3415 次点击
    这是一个创建于 2838 天前的主题,其中的信息可能已经有所发展或是发生改变。

    静态的 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;
    }
    
    
    
    4 条回复    2016-07-29 14:02:37 +08:00
    expkzb
        1
    expkzb  
       2016-07-19 21:00:28 +08:00
    你放个 stackview 不行么,没有这么玩的
    jiangdaohong
        2
    jiangdaohong  
       2016-07-20 11:27:03 +08:00
    为什么这么搞啊,直接做成两个 section 不可以吗
    ma125125t
        3
    ma125125t  
       2016-07-20 15:56:01 +08:00
    用 childController 吧...
    Tangdixi
        4
    Tangdixi  
       2016-07-29 14:02:37 +08:00
    tableView 里面嵌套 tableView 想想就觉得蛋疼啊 ......
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2852 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 02:52 · PVG 10:52 · LAX 19:52 · JFK 22:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.