静态UITableViewCell中的动态UITableView [英] dynamic UITableView in static UITableViewCell

查看:77
本文介绍了静态UITableViewCell中的动态UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下设置会导致麻烦:

在我的故事板中,我有一个标准的表视图控制器,包含一个包含三个静态单元作为内容的表视图。其中一个单元格包含另一个包含动态内容的表视图,其中包含原型单元格。表视图的委托和数据源都设置为同一个表视图控制器。在委托和数据源方法中,我确定哪个tableview调用了方法并适当地处理响应。但是,如果动态表视图包含多个元素,我会得到 NSRangeException ,其中包含 [__ NSArrayI objectAtIndex:]:索引1超出边界[0 .. 0] 。我无法追踪它,因为callstack中没有任何东西,我看起来很熟悉。我怀疑我的数据已经破了但是在使用 NSLog 消息进行污染后,它似乎不是这样,我仍然很丢失。

In my storyboard I have a standard table view controller, containing a table view with three static cells as content. One of the cells contains another table view with dynamic content and a prototype cell in it. Both table views' delegate and datasource are set to the same table view controller. In the delegate and datasource methods I determine which tableview has called the method and handle the response appropriately. However, if the dynamic table view contains more than one element I get a NSRangeException with [__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]. I was not able to track it down because the callstack has nothing in it which looks familiar to me. I suspected that my data was currupt but after polluting everthing with NSLog messages, it doesn't seem that way and I am still pretty lost.

这是我的代码:

@interface CartViewController : UITableViewController

@property (nonatomic, strong) IBOutlet UITableView* cartTableView;
@property (nonatomic, strong) IBOutlet UILabel* lblTotalPrice;
@property (nonatomic, strong) IBOutlet UITableViewCell* cellProductList;
@property (nonatomic, strong) IBOutlet UITableViewCell* cellTotalPrice;
@property (nonatomic, strong) IBOutlet UITableViewCell* cellProceedToCheckout;

@end

@implementation CartViewController

@synthesize cartTableView;
@synthesize lblTotalPrice;
@synthesize cellProductList;
@synthesize cellTotalPrice;
@synthesize cellProceedToCheckout;

// other methods ...

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[cartTableView layer] setCornerRadius:10];
    [cartTableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    [cartTableView setClipsToBounds:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [cartTableView reloadData];
    lblTotalPrice.text = [NSString stringWithFormat:@"%.2f €", [[DataStorage instance] shoppingCartTotalPrice]];
    [super viewWillAppear:animated];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSString* tableName = @"unknown";
    if(tableView == cartTableView)
        tableName = @"dynamic";
    if(tableView == self.tableView)
        tableName = @"static";

    int sections = 0;

    if (tableView == self.tableView)
        sections = 3;
    if (tableView == cartTableView)
        sections = 1;

    NSLog(@"%@ sections %d", tableName, sections);
    return sections;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString* tableName = @"unknown";
    if(tableView == cartTableView)
        tableName = @"dynamic";
    if(tableView == self.tableView)
        tableName = @"static";

    int rows = 0;

    if (tableView == self.tableView && section < 3)
        rows = 1;
    if (tableView == cartTableView && section == 0)
        rows = [[DataStorage instance] shoppingCartItemCount];

    NSLog(@"%@ rows %d", tableName, rows);
    return rows;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    float height = 44;
    if (tableView == self.tableView)
    {
        if(indexPath.section == 0)
            height = UIInterfaceOrientationIsLandscape(self.interfaceOrientation)
            ? kCellProductListHeightLandscape
            : kCellProductListHeightPortrait;
    }
    else
    {
        height = kCartItemCellHeight;
    }

    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = nil;

    NSString* tableName = @"unknown";
    if(tableView == cartTableView)
        tableName = @"dynamic";
    if(tableView == self.tableView)
        tableName = @"static";

    NSLog(@"%@ indexpath row %d section %d", tableName, indexPath.row, indexPath.section);

    if(tableView == cartTableView && indexPath.section == 0)
    {        
        ProductDefaultCell* cartItemCell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
        if (cartItemCell == nil) {
            cartItemCell = [[ProductDefaultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];
        }

        DataStorage* dataStorage = [DataStorage instance];

        ProductEntity* product = [dataStorage productFromCartAtIndex:indexPath.row];

        // set up cell data from product ...

        cell = cartItemCell;
    }
    else
    {
        switch (indexPath.section)
        {
            case 0:
                cell = cellProductList;
                break;
            case 1:
                cell = cellTotalPrice;
                break;
            case 2:
                cell = cellProceedToCheckout;
                break;
            default:
                break;
        }
    }

    NSLog(@"%@ %@", tableName, cell == nil ? @"error" : @"ok");

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* tableName = @"unknown";
    if(tableView == cartTableView)
        tableName = @"dynamic";
    if(tableView == self.tableView)
        tableName = @"static";

    NSLog(@"%@ display row %d section %d", tableName, indexPath.row, indexPath.section);
}

这是控制台输出:

2012-04-12 09:58:51.842 MyApp[363:fb03] dynamic sections 1
2012-04-12 09:58:51.856 MyApp[363:fb03] dynamic rows 2
2012-04-12 09:58:51.857 MyApp[363:fb03] dynamic sections 1
2012-04-12 09:58:51.857 MyApp[363:fb03] dynamic rows 2
2012-04-12 09:58:51.859 MyApp[363:fb03] static sections 3
2012-04-12 09:58:51.860 MyApp[363:fb03] static sections 3
2012-04-12 09:58:51.860 MyApp[363:fb03] static rows 1
2012-04-12 09:58:51.861 MyApp[363:fb03] static rows 1
2012-04-12 09:58:51.861 MyApp[363:fb03] static rows 1
2012-04-12 09:58:51.862 MyApp[363:fb03] static indexpath row 0 section 0
2012-04-12 09:58:51.862 MyApp[363:fb03] static ok
2012-04-12 09:58:51.863 MyApp[363:fb03] dynamic sections 1
2012-04-12 09:58:51.864 MyApp[363:fb03] dynamic rows 2
2012-04-12 09:58:51.864 MyApp[363:fb03] dynamic indexpath row 0 section 0
2012-04-12 09:58:51.978 MyApp[363:fb03] dynamic ok
2012-04-12 09:58:51.981 MyApp[363:fb03] dynamic display row 0 section 0
2012-04-12 09:58:51.991 MyApp[363:fb03] dynamic indexpath row 1 section 0
2012-04-12 09:58:52.053 MyApp[363:fb03] dynamic ok
objc[363]: EXCEPTIONS: throwing 0x6b6f670 (object 0x6b6fc20, a NSException)
objc[363]: EXCEPTIONS: searching through frame [ip=0x23d8678 sp=0xbfffc1a0] for exception 0x6b6f650
objc[363]: EXCEPTIONS: searching through frame [ip=0x23e2578 sp=0xbfffc1d0] for exception 0x6b6f650
objc[363]: EXCEPTIONS: searching through frame [ip=0x23e2673 sp=0xbfffd230] for exception 0x6b6f650
objc[363]: EXCEPTIONS: searching through frame [ip=0x23d8678 sp=0xbfffd4f0] for exception 0x6b6f650
objc[363]: EXCEPTIONS: searching through frame [ip=0x23e2578 sp=0xbfffd520] for exception 0x6b6f650
objc[363]: EXCEPTIONS: terminating
objc[363]: EXCEPTIONS: searching through frame [ip=0x1a0deed sp=0xbfffb6a0] for exception 0x6b6f650
objc[363]: EXCEPTIONS: catch(id)
objc[363]: EXCEPTIONS: unwinding through frame [ip=0x1a0deed sp=0xbfffb6a0] for exception 0x6b6f650
objc[363]: EXCEPTIONS: handling exception 0x6b6f650 at 0x1a0deff
2012-04-12 09:58:52.139 MyApp[363:fb03] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(0x187c022 0x1a0dcd6 0x1868644 0x8a7f88 0x6b41eb 0x51c008 0x51c3ce 0x507cbd 0x5166f1 0x4bfd42 0x187de42 0x23d8679 0x23e2579 0x23e2674 0x4b9967 0x670033 0x51be26 0x51c3ce 0x507cbd 0x5166f1 0x4bfd21 0x187de42 0x23d8679 0x23e2579 0x23674f7 0x23693f6 0x23f6160 0x491f30 0x185099e 0x17e7640 0x17b34c6 0x17b2d84 0x17b2c9b 0x1c7a7d8 0x1c7a88a 0x481626 0x247d 0x23e5)


推荐答案

我从未弄清楚是什么导致了这个问题,而是我将我的TableViewCell(包含动态标签的那个)子类化了le)让它实现必要的协议,并将表视图委托和数据源分配给它,而不是静态表视图的视图控制器。这非常合适,无论如何都可能是更好的设计。

I never figured out what was causing the problem, but instead I subclassed my TableViewCell (the one that contains the dynamic table) and let it implement the neccessary protocols and assigned the table views delegate and datasource to it, instead of the view controller of the static table view. That works perfectly fine and is probably better design anyway.

以下是一些示例代码:

@interface CellWithTableInside : UITableViewCell <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView* tableViewInCell;
@property (nonatomic, strong) NSArray* cellData;

@end

@implementation CellWithTableInside

@synthesize tableViewInCell;
@synthesize cellData = _cellData;

- (void)setCellData:(NSArray *)cellData
{
    _cellData = cellData;
    [tableViewInCell reloadData];
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [tableViewInCell setFrame:self.bounds];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupTableView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupTableView];
    }
    return self;
}

- (void)setupTableView
{
    tableViewInCell = [[UITableView alloc] initWithFrame:self.bounds];
    [tableViewInCell setDataSource:self];
    [tableViewInCell setDelegate:self];
    [self addSubview:tableViewInCell];
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cellData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NestedCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [self.cellData objectAtIndex:indexPath.row];
    return cell;
}

@end

您可以使用 CellWithTableInside 像这样:

@interface HostingTableViewController : UITableViewController

@property (nonatomic, strong) NSArray* tableData;

@end

@implementation HostingTableViewController

@synthesize tableData;

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}

- (void)viewDidLoad
{
    tableData = [NSArray arrayWithObjects:
                 [NSArray arrayWithObjects:@"February", nil],
                 [NSArray arrayWithObjects:@"Apple", @"Banana", nil],
                 [NSArray arrayWithObjects:@"John", @"Peter", @"Edward", nil],
                 [NSArray arrayWithObjects:@"Yellow", @"Green", @"Red", @"Blue", nil],
                 nil];
}

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tableData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44 * [(NSArray*)[tableData objectAtIndex:indexPath.row] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellWithTableInside";
    CellWithTableInside *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[CellWithTableInside alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    [cell setCellData:[tableData objectAtIndex:indexPath.row]];
    return cell;
}

@end

这适用于两者,动态或静态表视图。

This works for both, dynamic or static table views.

这篇关于静态UITableViewCell中的动态UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆