如何阻止UITableView单元在视图更改时消失

时间:2015-02-26 17:43:24

标签: ios objective-c uitableview uiview

使用addSubView更改容器视图时,我遇到了一个奇怪的错误,UITableView的内容消失了。

这个UITableView是动态的,我是从数组中设置单元格的。目前没有使用数组的内容,它只计算数组的长度并生成该数量的单元格。

start of this video可以看出,表视图在第一个视图中加载正常。但是当我将容器视图的内容更改为另一个视图然后再返回时,单元格似乎加载,然后消失!

我使用自定义Segue使用以下代码更改菜单按钮点击次数

-(void) perform {
    HomeViewController *src = (HomeViewController *)[self sourceViewController];
    UIViewController *dst = (UIViewController *)[self destinationViewController];

    //Clear view of all subviews
    for (UIView *view in src.placeholderView.subviews) {
        [view removeFromSuperview];
    }
    //save destination
    src.currentViewController = dst;

    //set placeholderOutlet to destination
    [src.placeholderView addSubview:dst.view];
 }

UITableViewController中的代码如下。

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;


    NSMutableArray *thisBlocks = [NSMutableArray arrayWithCapacity:7];

    Blocks *block = [[Blocks alloc] init];
    block.ID = @"232323";
    block.Name = @"128 Some Road";

    [thisBlocks addObject:block];

    block = [[Blocks alloc] init];
    block.ID = @"140332";
    block.Name = @"12 Another Road";
    [thisBlocks addObject:block];

    block = [[Blocks alloc] init];
    block.ID = @"889752";
    block.Name = @"Quadrant Square";
    [thisBlocks addObject:block];

    block = [[Blocks alloc] init];
    block.ID = @"1403323";
    block.Name = @"12 Some Road";
    [thisBlocks addObject:block];

    block = [[Blocks alloc] init];
    block.ID = @"8897522";
    block.Name = @"Quadrant Square";
    [thisBlocks addObject:block];

    block = [[Blocks alloc] init];
    block.ID = @"1403392";
    block.Name = @"12 Another Road";
    [thisBlocks addObject:block];

    block = [[Blocks alloc] init];
    block.ID = @"8897520";
    block.Name = @"Quadrant Square";
    [thisBlocks addObject:block];

    self.blocks = thisBlocks;

    //NSLog(@"array: %@", self.blocks);

    //NSLog(@"Rows : %lu", (unsigned long)[self.blocks count] );
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.

    return [self.blocks count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BlockCell"];


    return cell;
}

这真让我难过。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

rdelmar得到了它。我让我的ViewController设置了一个(弱,非原子)。将它改为`(强,非原子)修复了这个问题。谢谢 - Glenn Flanagan 43秒前编辑