在iOS 7中点击UITableView时,所有UITableCells都消失 [英] All UITableCells disappear when tapping on UITableView in iOS 7

查看:44
本文介绍了在iOS 7中点击UITableView时,所有UITableCells都消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS7中的UITableView遇到问题.最初,数据加载正常,然后我在控制台中获得输出,证明单元格正在正确地与数据源对话,,但是当我在表上的任意位置点击时,单元格就会消失并且表会消失空白.空UITableView中的单元格高度似乎符合我自定义的原型单元格的高度(410px),但是单元格中的所有数据均消失了,并且空表视图的行为就像它只有一个单元格一样(如其默认值)状态,然后再连接到委托).

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.

为了获得一些背景信息,该应用程序类似于iPhone Instagram应用程序,我正在使用此应用程序作为学习iOS 7开发的方式.我一直在努力解决这个问题,但我找不到任何可以帮助我解决此问题的在线资源,所以我想问一下Stack Overflow上的所有聪明人.

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

此处为高分辨率版本

这是我的TableViewController代码:

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");
}

这是自定义单元代码PA_PhotoCell(合并的.h和.m文件):

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");
}

您可以看到一些NSLog()呼叫,以帮助我查看是否有任何呼叫.

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

我要去哪里错了?最终目标是单击TableViewCell实例之一并启动UINavigationController,我知道该怎么做,但是直到我弄清楚为什么UITableView不会滚动以及为什么消失后,我才能继续执行该步骤.当我点击它!

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!

经过大量的测试,调试和实验,我可以得出结论,实际上问题根本不在UITableView上,而是实际上,如何将UITableView加载到其父视图中存在问题.我仍然没有找到解决问题的方法,但是我越来越接近找到原因了.这是我发现的:

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:

首先,当点击屏幕底部的任何UIButton时(请参阅照片参考),它将UIViewController的相关实例加载到名为placeholderView的UIView中.当我在该UIView的外部运行有问题的UITableView时(该UITableViewController自行运行,未嵌入另一个UIView中),该表可以正常工作,滚动,恢复点击事件,依此类推.因此,一旦我将UITableView加载到UIView中,UITableView就会变得无响应(它不会滚动或接收点击事件),并且尝试与之交互,UITableView就会完全空白.我的调试会话得出的结论是,NSArray * photos永远不会重置为nil,或者无论如何都无法对其进行操作,因此表只是空白.

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.

因此,对于将UITableView加载到通用UIView时会导致UITableView执行此操作的人是否有任何想法?加载到该通用UIView中的所有其他视图都是响应性的,并且行为符合预期.它只是这个UITableView给我的问题.

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.

如果您查看我所附的图形(上),您将看到我正在使用似乎是UITabBarView的图形,但实际上,它只是一个内部带有UIButtons的通用视图.我之所以决定制作自己的"UITabBarView外观"而不是使用现成的UITAbBarView类,是因为我想为左下角的菜单"按钮提供自定义功能(我希望将一个不错的UIView滑入从左侧开始,并在点击菜单"按钮时从屏幕右侧停止约60像素,而且我不知道如何自定义UITabBarView的行为,因此我选择了这种方法.

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.

以下是将UITableViewController实际加载到子视图中的代码(通过CustomStoryboardSegway):

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   

,这是我的PA_HomeViewController的头文件(该视图包含"placeholderView",这是目标视图,在用户点击视图底部的UIButton之后,该视图将加载各种UIViewController(类似于TabBarView)) :

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

我希望在将UITableView加载到placeholderView的过程中遗漏一些明显的东西,并且其中的某些东西导致UITableView完全空白.

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.

推荐答案

在其他视图中显示UITableView时,必须始终确保托管" UITableView的视图控制器具有强引用到它的控制器.在您的情况下,UITableView的数据源似乎在添加UITableView作为子视图之后被释放了.

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.

将currentViewController属性从weak更改为strong应该可以解决您的问题.

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

这篇关于在iOS 7中点击UITableView时,所有UITableCells都消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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