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

时间:2016-03-25 19:19:45

标签: ios objective-c uitableview unwind-segue

我在界面构建器中通过segue连接了MAIN相机View(相机)和TABLE视图(照片列表)。 对于表View,我决定自己构建它(内部有TableView的UIViewController,单元格从带有自己类的XIB加载)

当我运行应用程序并从Camera视图行进到Table视图时,一切正常,单元格背景颜色显示,列表也可以滚动而没有问题。 (重复使用细胞没问题)

然后我将segue退回到Camera视图并再次返回Table视图,单元格数量正确但所有都是空单元格。

我为每个单元格安装了NSLog,框架已经更改,并且autoSize消失了。为什么会这样?我解开segue时是否需要做一些清理,以便下次第一次从头开始启动UIViewController?

任何提示都将不胜感激。 谢谢 !

// 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>>

[更新]源代码

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

[更新完整.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

这里是TableController的出口连接 enter image description here

和galleryCell.XIB的出口连接

enter image description here

1 个答案:

答案 0 :(得分:0)

好的,这是什么工作 这条线不会加载XIB

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

替换.m文件中的这行代码

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

然后它运行没有问题。