简体   繁体   English

UITableViewController在重新访问时不显示单元格

[英]UITableViewController not showing up cells when re-visit

I have a MAIN camera View (camera) and a TABLE view (photo list) connected by segue in interface builder. 我在界面构建器中通过segue连接了一个主相机视图(相机)和一个表格视图(照片列表)。 For the table View I decided to build it on my own (UIViewController with TableView inside, cells are loaded in from a XIB with its own class) 对于表View,我决定自己构建它(内部有TableView的UIViewController,从带有自己类的XIB加载单元格)

When I run the application and travel from Camera view to Table view, everything works fine, cell background colour is shown, list can be scrolled with no problems too. 当我运行应用程序并从Camera视图行进到Table视图时,一切正常,单元格背景颜色显示,列表也可以滚动而没有问题。 (Reuse cells no problems) (重复使用单元没有问题)

Then I unwind segue back to the Camera view and come back to Table view again, the number of cells is correct but all are empty cells. 然后我将segue展开回到Camera视图并再次返回Table视图,单元格数量正确但所有都是空单元格。

I have NSLog for each cells, the frame has been changed, and autoSize disappeared. 我有每个单元格的NSLog,框架已更改,并且autoSize消失了。 Why would that happen ? 为什么会这样? Do I need to do some clean up when I unwind the segue so the next time it will start the UIViewController from scratch like the first time ? 我解开segue时是否需要做一些清理,以便下次第一次从头开始启动UIViewController?

any hints would be appreciated. 任何提示将不胜感激。 Thank you ! 谢谢 !

// FIRST TIME TO TABLE VIEW
<galleryCell: 0x17516300; baseClass = UITableViewCell; frame = (0 0; 480 77); autoresize = RM+BM; layer = <CALayer: 0x175166a0>>

// SECOND TIME TO TABLE VIEW 
<galleryCell: 0x175a4910; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x17513d10>>

[UPDATE] source code [更新]源代码

// TableController.h
#import <UIKit/UIKit.h>
@interface TableController : UIViewController <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak) IBOutlet UITableView *tv;
@property (nonatomic,strong)  NSArray *data;
@end

[UPDATE full .m file] [更新完整的.m文件]

//
//  TableController.m
//  blurCam
//
//  Created by KDS on 3/25/16.
//  Copyright (c) 2016 KDS. All rights reserved.
//

#import "TableController.h"
#import "galleryCell.h"
#import "UIButtonExt.h"

BOOL nibRegistered=NO;

@implementation TableController

+(UIBarButtonItem*) backButton: (id)target selector:(SEL)selector{

    UIViewController* controller=(UIViewController*)target;
    [controller.navigationController setNavigationBarHidden:NO];

    UIImage* backImg = [UIImage imageNamed:@"detail_back.png"];
    UIButtonExt* backBtn = [UIButtonExt buttonWithType:UIButtonTypeCustom];
    [backBtn setFrame:CGRectMake(0, 0, backImg.size.width/3.0 , backImg.size.height/3.0)];
    [backBtn setImage:backImg forState:UIControlStateNormal];
    [backBtn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

    return [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [[self navigationController] setNavigationBarHidden:NO animated:YES];
    self.navigationItem.leftBarButtonItem=[TableController backButton:self selector:@selector(back)];

    // get document folder file list
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    _data=[self listFileAtPath:documentsDirectory];

    [self.tv reloadData];
}

-(void) viewWillAppear:(BOOL)animated {
    [_tv reloadData];
}

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

-(NSArray *)listFileAtPath:(NSString *)path
{
    //-----> LIST ALL FILES <-----//
    NSLog(@"LISTING ALL FILES FOUND");

    int count;
    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
    return directoryContent;
}

// LAYER TABLE !
//
#pragma mark-
#pragma mark Table view data source method


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_data count];
}

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

    // set a tag
    static NSString *iden=@"galleryCell";

    // use nib as cell design

    if(nibRegistered==NO) {
        UINib *nib=[UINib nibWithNibName:@"galleryCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:iden];
        nibRegistered=YES;
    }

    // ask for reuse ?
    galleryCell *cell=(galleryCell*)[tableView dequeueReusableCellWithIdentifier:iden];
    if(cell == nil){
        NSLog(@"NILLLLLL");
        cell = [[galleryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }

    cell.btn1.hidden=NO;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *imgPath = [documentsDirectory stringByAppendingPathComponent:[_data objectAtIndex:0]];
    NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:imgPath]];

    UIImage *img=[UIImage imageWithData:imgData];
    NSLog(@"image to load %@",imgPath);

    [cell.btn1 setImage:img forState:UIControlStateNormal];

    NSLog(@"~~~%@",cell);
    NSLog(@"~~~hidden %d",cell.hidden);


    return cell;

}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       return 77;
}

//
//
//
//

- (BOOL)prefersStatusBarHidden { return YES; }

//

-(void) back {
    [self performSegueWithIdentifier: @"cameraSegue" sender: self];
}


@end

and here's the outlet connections of TableController 这是TableController的出口连接 在此输入图像描述

and the outlet Connection of galleryCell.XIB 和galleryCell.XIB的出口Connection

在此输入图像描述

OK here's what's working this line won't load the XIB 好的,这是什么工作,这一行不会加载XIB

cell = [[galleryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];

replace this line of code in the .m file 替换.m文件中的这行代码

cell = [[[NSBundle mainBundle] loadNibNamed:@"galleryCell" owner:self options:nil] objectAtIndex:0]; 

then it is running with no problems. 然后它运行没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM