Ios iPhone 5 UITableView未显示
我目前正在开发一个具有许多UITableView的应用程序,我通过在iPhone 6上测试创建了该应用程序。我最近开始在其他设备上进行测试,我在iPhone5上注意到,任何包含多个部分的UITableView都会跳过第0部分,而不会显示它。虽然在我的iphone6和iphone5s上,它会显示得非常好。下面是一些用于显示“我的个人资料”部分的UITableView的代码。任何帮助都将不胜感激。值得一提的是,所有设备都运行9.1。谢谢Ios iPhone 5 UITableView未显示,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我目前正在开发一个具有许多UITableView的应用程序,我通过在iPhone 6上测试创建了该应用程序。我最近开始在其他设备上进行测试,我在iPhone5上注意到,任何包含多个部分的UITableView都会跳过第0部分,而不会显示它。虽然在我的iphone6和iphone5s上,它会显示得非常好。下面是一些用于显示“我的个人资料”部分的UITableView的代码。任何帮助都将不胜感激。值得一提的是,所有设备都运行9.1。谢谢 #pragma mark - UITableViewDataS
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 2;
} else if (section == 1) {
if ([_selectedSection isEqualToString:@"captures"]) {
if ([_capturesArray count] > 0) {
return [_capturesArray count];
} else {
return 1;
}
} else if ([_selectedSection isEqualToString:@"comments"]) {
if ([_commentsArray count] > 0) {
return [_commentsArray count];
} else {
return 1;
}
} else if ([_selectedSection isEqualToString:@"followers"]) {
if ([_followersArray count] > 0) {
return [_followersArray count];
} else {
return 1;
}
} else if ([_selectedSection isEqualToString:@"following"]) {
if ([_followingArray count] > 0) {
return [_followingArray count];
} else {
return 1;
}
}
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath == [NSIndexPath indexPathForRow:0 inSection:0]) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userCell" forIndexPath:indexPath];
return cell;
} else if (indexPath == [NSIndexPath indexPathForRow:1 inSection:0]) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"menuCell" forIndexPath:indexPath];
return cell;
} else if (indexPath.section == 1){
if ([_selectedSection isEqualToString:@"captures"] && [_capturesArray count] > 0) {
// Get Capture
Capture *capture = _capturesArray[indexPath.row];
// Create Cell
NSString *cellID = capture.cellID;
CaptureTableViewCells *cell = (CaptureTableViewCells *)[tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CaptureCells" owner:nil options:nil];
if ([capture.cellID isEqualToString:FeaturedCellID]) {
cell = (CaptureTableViewCells *)[nib objectAtIndex:0];
} else {
cell = (CaptureTableViewCells *)[nib objectAtIndex:1];
}
}
return cell;
} else if ([_selectedSection isEqualToString:@"comments"] && [_commentsArray count] > 0) {
} else if ([_selectedSection isEqualToString:@"followers"] && [_followersArray count] > 0) {
} else if ([_selectedSection isEqualToString:@"following"] && [_followingArray count] > 0) {
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"emptyCell" forIndexPath:indexPath];
// Configure the cell...
return cell;
}
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userCell" forIndexPath:indexPath];
// Configure the cell...
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath == [NSIndexPath indexPathForRow:0 inSection:0]) {
UIImageView *profileImageView = (UIImageView *)[cell viewWithTag:10];
profileImageView.layer.borderColor = [self uicolorFromHex:0xffd700].CGColor;
profileImageView.layer.borderWidth = 5;
profileImageView.layer.cornerRadius = profileImageView.frame.size.width/2;
profileImageView.layer.masksToBounds = YES;
} else if (indexPath == [NSIndexPath indexPathForRow:1 inSection:0]) {
// Get Buttons
_capturesMenuButton = (UIButton *)[cell viewWithTag:1];
_commentsMenuButton = (UIButton *)[cell viewWithTag:2];
_followersMenuButton = (UIButton *)[cell viewWithTag:3];
_followingMenuButton = (UIButton *)[cell viewWithTag:4];
// Set Text Of Buttons
if ([_capturesArray count] > 0) {
[_capturesMenuButton setTitle:[NSString stringWithFormat:@"%lu", (unsigned long)[_capturesArray count]] forState:UIControlStateNormal];
}
// Set Buttons Selector
[_capturesMenuButton addTarget:self action:@selector(capturesMenuButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[_commentsMenuButton addTarget:self action:@selector(commentsMenuButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[_followersMenuButton addTarget:self action:@selector(followersMenuButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[_followingMenuButton addTarget:self action:@selector(followingMenuButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
} else if (indexPath.section == 1){
if ([_selectedSection isEqualToString:@"captures"]) {
if ([_capturesArray count] > 0) {
// Has Captures
CaptureTableViewCells *capCell = (CaptureTableViewCells *)cell;
// Get Capture At Index
Capture *capture = _capturesArray[indexPath.row];
capCell.capture = capture;
NSURLRequest *request = [NSURLRequest requestWithURL:capture.captureImage.imageURL];
UIImage *placeholderImage = [UIImage imageNamed:@"Overlay"];
__weak CaptureTableViewCells *weakCell = capCell;
[capCell.captureImageView setImageWithURLRequest:request
placeholderImage:placeholderImage
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
weakCell.captureImageView.image = image;
weakCell.capture.captureImage.image = weakCell.captureImageView.image;
[weakCell.activityIndicator stopAnimating];
[weakCell setNeedsLayout];
} failure:nil];
} else {
// No Captures
UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = @"You have no Captures.";
}
} else if ([_selectedSection isEqualToString:@"comments"]) {
if ([_commentsArray count] > 0) {
// Has Captures
} else {
// No Captures
UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = @"You have no comments.";
}
} else if ([_selectedSection isEqualToString:@"followers"]) {
if ([_followersArray count] > 0) {
// Has Captures
} else {
// No Captures
UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = @"You have no followers.";
}
} else if ([_selectedSection isEqualToString:@"following"]) {
if ([_followingArray count] > 0) {
// Has Captures
} else {
// No Captures
UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = @"You are not following anyone.";
}
}
}
// Remove seperator inset
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
// Prevent the cell from inheriting the Table View's margin settings
if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
[cell setPreservesSuperviewLayoutMargins:NO];
}
// Explictly set your cell's layout margins
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
// Setup Style For Cell
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath == [NSIndexPath indexPathForRow:0 inSection:0]) {
return 208;
} else if (indexPath == [NSIndexPath indexPathForRow:1 inSection:0]) {
return 72;
} else if (indexPath.section == 1){
if ([_selectedSection isEqualToString:@"captures"]) {
if ([_capturesArray count] > 0) {
Capture *capture = _capturesArray[indexPath.row];
return capture.cellHeight;
} else {
return self.view.frame.size.height - 280 - self.navigationController.navigationBar.frame.size.height - self.tabBarController.tabBar.frame.size.height;
}
} else if ([_selectedSection isEqualToString:@"comments"]) {
if ([_commentsArray count] > 0) {
Capture *capture = _commentsArray[indexPath.row];
return capture.cellHeight;
} else {
return self.view.frame.size.height - 280 - self.navigationController.navigationBar.frame.size.height - self.tabBarController.tabBar.frame.size.height;
}
} else if ([_selectedSection isEqualToString:@"followers"]) {
if ([_followersArray count] > 0) {
return 0;
} else {
return self.view.frame.size.height - 280 - self.navigationController.navigationBar.frame.size.height - self.tabBarController.tabBar.frame.size.height;
}
} else if ([_selectedSection isEqualToString:@"following"]) {
if ([_followingArray count] > 0) {
return 0;
} else {
return self.view.frame.size.height - 280 - self.navigationController.navigationBar.frame.size.height - self.tabBarController.tabBar.frame.size.height;
}
}
}
return 0;
}
更新
所以我注释掉了tableView中的所有内容:heightForRowAtIndexPath:并返回了100作为每个单元格的高度。现在我的第0节单元格可见。当我添加日志语句以查看发生了什么时,这就是我得到的结果
这是我的代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"IndexPath: %ld, %ld", (long)indexPath.section, (long)indexPath.row);
if (indexPath == [NSIndexPath indexPathForRow:0 inSection:0]) {
NSLog(@"Setting Height");
return 208;
}
这是我的日志:
2015-12-14 13:27:21.678 Captures[324:42956] IndexPath: 0, 0
2015-12-14 13:27:21.678 Captures[324:42956] IndexPath: 0, 1
如您所见,它直接跳过设置第0节第0行的高度。在我的iphone6和iPhone5s上,这种情况不会发生。有人知道为什么会发生这种情况吗?我创建了一个小测试应用程序来确认:您不应该使用
=
操作符来比较nsindepath
对象,就像您在代码中一样。虽然它似乎在我的iPhone6模拟器上工作,但在iPhone5模拟器上却不工作
与其检查这两个对象是否相等,不如使用compare:
方法,或者检查nsindepath
的各个属性
根据苹果的文档,compare:
似乎是首选:
如果不一定同时关心部分
和行
属性,还可以检查nsindepath
的各个属性
if(indexPath.row == 0 && indexPath.section == 0)
// do something
我创建了一个小测试应用程序来确认:您不应该使用
=
操作符来比较nsindepath
对象,就像您在代码中一样。虽然它似乎在我的iPhone6模拟器上工作,但在iPhone5模拟器上却不工作
与其检查这两个对象是否相等,不如使用compare:
方法,或者检查nsindepath
的各个属性
根据苹果的文档,compare:
似乎是首选:
如果不一定同时关心部分
和行
属性,还可以检查nsindepath
的各个属性
if(indexPath.row == 0 && indexPath.section == 0)
// do something
你是否检查了视图调试器,如果该部分被移到你看不到的最顶部?你是在序列图像板中还是在代码中创建表视图?您使用的是自动布局吗?@T先生,我可以在屏幕顶部看到我的第0节标题,下面什么都没有。它只是跳到了第1节。我不太熟悉视图调试器。我做了调试->查看调试->捕获视图层次结构。打开后,我单击了“显示剪辑内容”,据我所知,在我的第0节标题上方没有任何内容。@AdeelMiraj我正在使用自动布局的故事板。是否尝试记录每个单元格的高度?是否检查了视图调试器,如果该部分被移到顶部看不到它,您是在序列图像板中还是在代码中创建表视图?您使用的是自动布局吗?@T先生,我可以在屏幕顶部看到我的第0节标题,下面什么都没有。它只是跳到了第1节。我不太熟悉视图调试器。我做了调试->查看调试->捕获视图层次结构。打开后,我单击“显示剪辑内容”据我所知,我的0节标题上方没有任何内容。@AdeelMiraj我正在使用自动布局的故事板。您是否尝试记录每个单元格的高度?谢谢@Stonz2,我做了您建议的更改,效果很好。谢谢@Stonz2,我做了您建议的更改,效果很好。