Ich*_*aki 9 objective-c uitableview ios uistoryboard ios7
我在iOS7中遇到了UITableView的问题.最初,数据加载得很好,我在控制台中输出,证明单元格正确地与数据源对话,但是当我点击表格上的任何位置时,单元格消失,表格变为空白.空UITableView中单元格的高度似乎是为了尊重我的自定义原型单元格(410px),但是单元格中的所有数据都消失了,而空表视图就像它只有一个单元格一样(就像它的默认值一样)在它被连接到代表之前的状态).
我正在为这个应用程序使用Storyboard.
为了获得一点上下文,这个应用程序类似于iphone Instagram应用程序,我正在使用这个应用程序来学习iOS 7开发.我一直在试图解决这个问题,我找不到任何可以帮助我解决这个问题的在线资源,所以我想问Stack Overflow上的所有智能窥视.
我准备了一个图形,可以帮助您查看问题
这是我的TableViewController代码:
@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");
}
Run Code Online (Sandbox Code Playgroud)
这是自定义单元格代码PA_PhotoCell(合并.h和.m文件):
@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");
}
Run Code Online (Sandbox Code Playgroud)
你可以看到几个NSLog()电话来帮助我看看是否有任何东西被调用.
任何人都可以帮我看看我哪里出错了?最终目标是点击其中一个TableViewCell实例并启动一个UINavigationController,我知道该怎么做,但我不能继续这一步,直到我弄清楚为什么我的UITableView不会滚动,以及为什么它会在我消失时消失点击它!请帮忙!
提前感谢任何能够提供帮助的人!
编辑:经过大量的测试,调试和实验,我已经能够得出结论,问题实际上根本不是UITableView,事实上,它是如何将UITableView加载到它的父视图中的问题.我仍然没有找到解决问题的办法,但我越来越接近找到原因.这是我发现的:
首先,当点击屏幕底部的任何UIButtons(参见照片参考)时,它会将UIViewController的相关实例加载到名为placeholderView的UIView中.当我运行这个UIView的有问题的UITableView OUTSIDE(其中UITableViewController独立运行,而不是嵌入在另一个UIView中)时,该表工作正常,它滚动,它恢复点击事件,依此类推.因此,只要我将UITableView加载到UIView中,UITableView就会无响应(它不会滚动或接收点击事件)以及任何与它交互的尝试,UITableView会完全空白.我的调试会话得出结论,NSArray*照片永远不会被重置为nil,或者无论如何被操纵,表格都是空白的.
那么有没有人有任何关于什么会导致UITableView加载到通用UIView时这样做的想法?加载到此通用UIView中的所有其他视图都是响应式的,并且按预期运行.只是这个UITableView给了我一些问题.
如果你查看我在这篇文章中附上的图片(上图),你会发现我正在使用看起来像UITabBarView的图片,但事实上,它只是一个内部有UIButtons的通用视图.我决定制作自己的"UITabBarView外观"而不是使用现成的UITAbBarView类的原因是因为我想给左下角的"菜单"按钮提供自定义功能(我想要一个漂亮的UIView滑入从左侧开始,当点击"菜单"按钮时,屏幕右侧停止约60个像素,我无法弄清楚如何自定义UITabBarView的行为,所以我选择了这种方法.
以下是实际将UITableViewController加载到子视图中的代码(通过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
Run Code Online (Sandbox Code Playgroud)
这是我的PA_HomeViewController的头文件(该视图包含"placeholderView",它是在用户点击视图底部的UIButtons后加载各种UIViewControllers的目标视图(类似于TabBarView):
@interface PA_HomeViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIView *placeholderView;
@property (weak, nonatomic) UIViewController *currentViewController;
@end
Run Code Online (Sandbox Code Playgroud)
我希望我只是错过了将UITableView加载到占位符视图中的方式,而且其中的某些内容导致UITableView完全空白.
我非常感谢大家到目前为止提供的所有建议.我真的很想找到这个问题的原因,所以我可以在晚上睡觉.哈哈.
当您UITableView在另一个视图中显示时,必须始终确保"托管"它的视图控制器UITableView具有对其控制器的强引用.在您的情况下,UITableView添加UITableViewas子视图后似乎已释放了数据源.
将currentViewController属性更改weak为strong应该可以解决您的问题.