V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
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 · Jul 19, 2016 · 4247 views
    This topic created in 3572 days ago, the information mentioned may be changed or developed.

    静态的 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 replies    2016-07-29 14:02:37 +08:00
    expkzb
        1
    expkzb  
       Jul 19, 2016
    你放个 stackview 不行么,没有这么玩的
    jiangdaohong
        2
    jiangdaohong  
       Jul 20, 2016
    为什么这么搞啊,直接做成两个 section 不可以吗
    ma125125t
        3
    ma125125t  
       Jul 20, 2016
    用 childController 吧...
    04BxPLXu2M6UKH6Z
        4
    04BxPLXu2M6UKH6Z  
       Jul 29, 2016
    tableView 里面嵌套 tableView 想想就觉得蛋疼啊 ......
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2490 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 05:32 · PVG 13:32 · LAX 22:32 · JFK 01:32
    ♥ Do have faith in what you're doing.