All UITableCells disappear when tapping on UITableView in iOS 7

ε祈祈猫儿з 提交于 2019-12-08 17:36:18

问题


I am having a problem with my UITableView in iOS7. Initially, the data loads in just fine, and I get output in my console that proves that the cells are speaking to the data source correctly, but as soon as I tap anywhere on the table, the cells disappear and the table goes blank. The height of the cells in the empty UITableView seem to be honoring the height my custom prototype cell (410px), but all the data in the cells vanish, and the empty table view acts like it only has one cell in it (like its default state before it gets hooked up to the delegate).

I am using Storyboards for this app.

To get a little context, this app is similar to the iphone Instagram app, and I am using this application as way to learn iOS 7 development. I have been banging my head up against a wall trying to solve this issue, and I can't find any online resources that can help me solve this, so I wanted to ask all the smart peeps on Stack Overflow.

I have prepared a graphic that helps you see the problem

higher resolution version here

Here is my TableViewController code:

@interface PA_PhotoTableViewController ()

@property (nonatomic, copy) NSArray *photos;

@end



@implementation PA_PhotoTableViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.photos = [[PA_PhotoStore sharedPhotoStore] allPhotos];
}


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


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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PA_PhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PhotoCell" forIndexPath:indexPath];


    PA_Photo *photo = (self.photos)[indexPath.row];

    cell.photoTitle.text = photo.title;
    cell.photoOwnerName.text = [NSString stringWithFormat:@"%@", photo.owner];
    cell.photoLikes.text = @"99";

    // Photo Image URL
    NSURL *photoImageURL = [NSURL URLWithString:photo.image_full_url];
    [cell.photoImage sd_setImageWithURL:photoImageURL placeholderImage:[UIImage imageNamed:@"lightGraySpinningLoader.gif"]];

    // Photo Owner Image
    [cell.photoOwnerImage sd_setImageWithURL:photoImageURL placeholderImage:[UIImage imageNamed:@"lightGraySpinningLoader.gif"]];


    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    // This code never gets called when I try to tap on a cell
    NSLog(@"A row was selected");
}


- (void)dealloc {
    NSLog(@"dealloc called in PA_PhotoTableViewController");
}

and here is the custom cell code PA_PhotoCell (consolidated .h & .m files):

@interface PA_PhotoCell : UITableViewCell

@property (nonatomic, weak) IBOutlet UIImageView *photoImage;
@property (nonatomic, weak) IBOutlet UILabel *photoTitle;
@property (nonatomic, weak) IBOutlet UILabel *photoOwnerName;
@property (nonatomic, weak) IBOutlet UIImageView *photoOwnerImage;
@property (nonatomic, weak) IBOutlet UILabel *photoLikes;
@property (nonatomic, weak) IBOutlet UILabel *photoTimestamp;
@property (nonatomic, weak) IBOutlet UILabel *photoComments;

@end



@implementation PA_PhotoCell

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    NSLog(@"in set selected");
}


-(void)setHighlighted:(BOOL)highlighted {
    NSLog(@"in set highlighted");
}

You can see a few NSLog() calls to help me see if anything is getting called.

Where am I going wrong? The end goal is to click on one of the TableViewCell instances and launch a UINavigationController, I know how to do that, but I can't move on to that step until I figure out why my UITableView won't scroll, and why it disappears when I click on it!

EDIT: After much testing, debugging and experimentation, I have been able to conclude that the problem is actually not with the UITableView at all, and it is, in fact, a problem with how the UITableView is being loaded into its parent view. I still haven't found a solution to my problem, but I am getting closer to finding the cause. Here is what I have discovered:

First, when any of the UIButtons at the bottom of the screen are tapped (see photo reference), it loads the relevant instance of UIViewController into a UIView called placeholderView. When I run my problematic UITableView OUTSIDE of this UIView (where the UITableViewController is acting on its own, not embedded within another UIView) then the table works perfectly, it scrolls, it revives click events, and so on. So as soon as I load the UITableView into the UIView, the UITableView becomes unresponsive (it doesn't scroll or receive tap events) and any attempt to interact with it, the UITableView goes completely blank. My debugging session concludes that the NSArray *photos never gets reset to nil, or manipulated in anyway, the table just goes blank.

So does anyone have any ideas on what would cause a UITableView to do this when being loaded into a generic UIView? All the other views that get loaded into this generic UIView are responsive, and behave as expected. Its just this UITableView that is giving me problems.

If you review the graphic I attached to this post (above), you will see that I am using what appears to be a UITabBarView, but it is, in fact, just a generic view with UIButtons inside. The reason I decided to craft my own "UITabBarView look-alike" instead of using the ready-made UITAbBarView class was because I wanted to give custom functionality to the "menu" button on the bottom left (I want a nice UIView to slide in from the left, and stop about 60 pixels from the right of the screen when the "menu" button is tapped, and I can't figure out how to customize the behavior of the UITabBarView, so I opted for this approach.

Here is the code that is actually loading the UITableViewController into the subview (via a CustomStoryboardSegway):

// PA_HomeViewCustomStoryboardSegue.m

#import "PA_HomeViewCustomStoryboardSegue.h"
#import "PA_HomeViewController.h"

@implementation PA_HomeViewCustomStoryboardSegue

// to create a custom segue, you have to override the perform method
-(void)perform {

    // get the source and destination view controllers
    PA_HomeViewController *segueSourceController = (PA_HomeViewController *)[self sourceViewController];
    UIViewController *destinationController = (UIViewController *)[self destinationViewController];

    for (UIView *view in segueSourceController.placeholderView.subviews){
        [view removeFromSuperview];
    }

    segueSourceController.currentViewController = destinationController;
    [segueSourceController.placeholderView addSubview:destinationController.view];

}

@end   

and here is the header file for my PA_HomeViewController (the view the contains the "placeholderView" which is the target view that loads the various UIViewControllers after the user has tapped the UIButtons at the bottom of the view (similar to a TabBarView) :

@interface PA_HomeViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIView *placeholderView;
@property (weak, nonatomic) UIViewController *currentViewController;

@end

I am hoping that I am just missing something obvious in the way that I am loading the UITableView into the placeholderView, and something in there is causing the UITableView to go completely blank.


回答1:


When you display the UITableView in a different view, you must always make sure that the view controller which "hosts" the UITableView has a strong reference to its controller. In your case, the data source for the UITableView seems to be deallocated after adding the UITableView as subview.

Changing the currentViewController property from weak to strong should fix your problem.




回答2:


In swift you need to declare viewcontroller object globally that would result in Strong, in case if you declare locally it results in keep disappearing the cells.

e.g.

var refineViewController : RefineViewController?

then you can access that controller using below code that would not result in disappearing cells.

func showRefineView(isFindHomeTab isFindHomeTab : Bool){


    refineViewController =   RefineViewController(nibName: String(BaseGroupedTableVC),bundle : nil)

    refineViewController!.refineSearchDelegate = self

    refineViewController!.view.frame = CGRectMake(0, -490, self.view.frame.size.width, self.view.frame.size.height)

    UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveEaseOut, animations:
        {

            self.refineViewController!.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
            self.refineViewController!.isFindHomeTab = isFindHomeTab

        }, completion: nil)

        self.view.addSubview(refineViewController!.view)

}



回答3:


I experienced the exact same problem. The issue was that I was using a custom datasource class (called tableVCDataSource), and was setting the tableView's dataSource incorrectly in the ViewController class. I was doing:

override func viewDidLoad() {
    mainTableView.dataSource = TableVCDataSource()
}

when I should have been doing:

fileprivate var tableVCDataSource: TableVCDataSource?

override func viewDidLoad() {
    tableVCDataSource = TableVCDataSource()
    mainTableView.dataSource = tableVCDataSource
}

This solved my issue.



来源:https://stackoverflow.com/questions/25560327/all-uitablecells-disappear-when-tapping-on-uitableview-in-ios-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!