所以我在导航栏中有一个带分段控件的uitableviewcontroller。我可以让tableview在索引0处第一次重新加载,第二次在索引1处重新加载。当我尝试返回第一个索引并且整个tableview消失时。
仅当分段控制索引为1且tableview部分2为空时才会出现此问题。如果该区域中有对象,它工作正常,我可以在两个tableviews之间来回切换。
我也意识到当我下拉刷新控件时,它说我在调试区域有一个空数组。我在刷新控制方法中放置断点,看看问题是什么,问题是在刷新控制方法被解雇之前......怎么样?
这不是一个重复的问题,我希望看到任何帮助,因为这是所有答案的地方。谢谢!
另外,整个uitableviewcontroller类非常长,所以如果你真的想看到它请问
NewsFeed.m文件https://dl.dropboxusercontent.com/u/10826637/NewsTableViewController.m
非常长
#import "NewsTableViewController.h"
#import "OtherNewsViewController.h"
#import "MSCellAccessory.h"
@interface NewsTableViewController ()
{
PFUser *_loggedInUser;
NSIndexPath *_newIndexPath;
PFObject *_clubInvite;
PFObject *_clubRequest;
UIRefreshControl *_refreshControl;
PFUser *_clubInviteUser;
PFUser *_clubRequestUser;
int _rowOneCount;
int _rowTwoCount;
NSMutableArray *_myFollowingArray;
}
@end
@implementation NewsTableViewController
- (void)viewDidLoad {
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButtonItem;
[self.segmentControl addTarget:self action:@selector(changedValue) forControlEvents:UIControlEventValueChanged];
_refreshControl = [[UIRefreshControl alloc] init];
[_refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:_refreshControl];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
_loggedInUser = [PFUser currentUser];
_myFollowingArray = [NSMutableArray arrayWithArray:_loggedInUser[@"Following"]];
if (![_myFollowingArray containsObject:_loggedInUser.username]) {
[_myFollowingArray addObject:_loggedInUser.username];
}
if (self.segmentControl.selectedSegmentIndex == 0) {
PFQuery *userQuery = [PFUser query];
[userQuery whereKey:@"username" containedIn:_myFollowingArray];
PFQuery *newsQuery = [PFQuery queryWithClassName:@"News"];
[newsQuery whereKey:@"Notified" matchesQuery:userQuery];
[newsQuery setLimit:50];
[newsQuery orderByDescending:@"createdAt"];
[newsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
self.followingNews = objects;
[self.tableView reloadData];
}];
} else if (self.segmentControl.selectedSegmentIndex == 1) {
PFQuery *inviteQuery = [PFQuery queryWithClassName:@"ClubInvites"];
[inviteQuery whereKey:@"Invited" equalTo:_loggedInUser];
[inviteQuery whereKey:@"Accepted" equalTo:[NSNumber numberWithBool:NO]];
[inviteQuery countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
_rowOneCount = number;
[self.tableView reloadData];
}];
PFQuery *requestsQuery = [PFQuery queryWithClassName:@"ClubRequests"];
[requestsQuery whereKey:@"Owner" equalTo:_loggedInUser];
[requestsQuery whereKey:@"Accepted" equalTo:[NSNumber numberWithBool:NO]];
[requestsQuery countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
_rowTwoCount = number;
[self.tableView reloadData];
}];
PFQuery *myNewsQuery = [PFQuery queryWithClassName:@"News"];
[myNewsQuery setLimit:50];
[myNewsQuery orderByDescending:@"createdAt"];
[myNewsQuery whereKey:@"Notified" equalTo:_loggedInUser];
[myNewsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
self.myNews = objects;
[self.tableView reloadData];
}];
} else if (self.segmentControl.selectedSegmentIndex == 2) {
[self.tableView reloadData];
}
}
- (void)changedValue {
if (self.segmentControl.selectedSegmentIndex == 0) {
PFQuery *userQuery = [PFUser query];
[userQuery whereKey:@"username" containedIn:_myFollowingArray];
PFQuery *newsQuery = [PFQuery queryWithClassName:@"News"];
[newsQuery setLimit:50];
[newsQuery orderByDescending:@"createdAt"];
[newsQuery whereKey:@"Notified" matchesQuery:userQuery];
[newsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
self.followingNews = objects;
[self.tableView reloadData];
}];
} else if (self.segmentControl.selectedSegmentIndex == 1) {
PFQuery *inviteQuery = [PFQuery queryWithClassName:@"ClubInvites"];
[inviteQuery whereKey:@"Invited" equalTo:_loggedInUser];
[inviteQuery whereKey:@"Accepted" equalTo:[NSNumber numberWithBool:NO]];
[inviteQuery countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
_rowOneCount = number;
[self.tableView reloadData];
}];
PFQuery *requestsQuery = [PFQuery queryWithClassName:@"ClubRequests"];
[requestsQuery whereKey:@"Owner" equalTo:_loggedInUser];
[requestsQuery whereKey:@"Accepted" equalTo:[NSNumber numberWithBool:NO]];
[requestsQuery countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
_rowTwoCount = number;
[self.tableView reloadData];
}];
PFQuery *myNewsQuery = [PFQuery queryWithClassName:@"News"];
[myNewsQuery setLimit:50];
[myNewsQuery orderByDescending:@"createdAt"];
[myNewsQuery whereKey:@"Notified" equalTo:_loggedInUser];
[myNewsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
self.myNews = objects;
[self.tableView reloadData];
}];
} else if (self.segmentControl.selectedSegmentIndex == 2) {
[self.tableView reloadData];
}
}
-(void)refresh:(UIRefreshControl *)refreshControl {
if (self.segmentControl.selectedSegmentIndex == 0) {
PFQuery *userQuery = [PFUser query];
[userQuery whereKey:@"username" containedIn:_loggedInUser[@"Following"]];
PFQuery *newsQuery = [PFQuery queryWithClassName:@"News"];
[newsQuery whereKey:@"Notified" matchesQuery:userQuery];
[newsQuery setLimit:50];
[newsQuery orderByDescending:@"createdAt"];
[newsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
self.followingNews = objects;
[self.tableView reloadData];
}];
[_refreshControl endRefreshing];
} else {
PFQuery *inviteQuery = [PFQuery queryWithClassName:@"ClubInvites"];
[inviteQuery whereKey:@"Invited" equalTo:_loggedInUser];
[inviteQuery whereKey:@"Accepted" equalTo:[NSNumber numberWithBool:NO]];
[inviteQuery countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
_rowOneCount = number;
[self.tableView reloadData];
}];
PFQuery *requestsQuery = [PFQuery queryWithClassName:@"ClubRequests"];
[requestsQuery whereKey:@"Owner" equalTo:_loggedInUser];
[requestsQuery whereKey:@"Accepted" equalTo:[NSNumber numberWithBool:NO]];
[requestsQuery countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
_rowTwoCount = number;
[self.tableView reloadData];
}];
PFQuery *myNewsQuery = [PFQuery queryWithClassName:@"News"];
[myNewsQuery setLimit:50];
[myNewsQuery orderByDescending:@"createdAt"];
[myNewsQuery whereKey:@"Notified" equalTo:_loggedInUser];
[myNewsQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
self.myNews = objects;
[_refreshControl endRefreshing];
[self.tableView reloadData];
}];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
if (self.segmentControl.selectedSegmentIndex == 0) {
return 1;
} else {
return 3;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.segmentControl.selectedSegmentIndex == 0) {
return self.followingNews.count;
} else {
if (section == 2) {
return self.myNews.count;
} else {
return 1;
}
}
}
//Long tableview configuration method for segumented controller
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = @"Cell";
NSString *cellIdentifier2 = @"Cell2";
UITableViewCell *cell = nil;
if (self.segmentControl.selectedSegmentIndex == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
self.cellImageView = (UIImageView *)[cell.contentView viewWithTag:1];
self.cellImageView.layer.cornerRadius = 6.0f;
self.cellImageView.clipsToBounds = YES;
//Create the cell label going into the cell
self.cellLabel = (UILabel *)[cell.contentView viewWithTag:3];
[cell.contentView addSubview:self.cellLabel];
[cell.contentView addSubview:self.cellImageView];
PFObject *eachNews = [self.followingNews objectAtIndex:indexPath.row];
PFUser *notifier = [eachNews objectForKey:@"Notifier"];
PFUser *notified = [eachNews objectForKey:@"Notified"];
[notifier fetchIfNeeded];
[notified fetchIfNeeded];
NSString *notifierString = [[NSString alloc] init];
NSString *notifiedString = [[NSString alloc] init];
NSString *grammer = [[NSString alloc] init];
NSDate *timeStamp = eachNews.createdAt;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM d, yyyy h:mm a"];
NSString *timeString = [dateFormatter stringFromDate:timeStamp];
if ([notifier.username isEqualToString:_loggedInUser.username]) {
notifierString = @"You";
grammer = @"are";
} else {
notifierString = notifier[@"username"];
grammer = @"is";
}
if ([notified.username isEqualToString: _loggedInUser.username]) {
notifiedString = @"you";
} else {
notifiedString = notified.username;
}
if (notifier[@"profileImage"] == nil) {
UIImage *hermet = [UIImage imageNamed:@"ohyeah.jpg"];
[self.cellImageView setImage:hermet];
} else {
PFFile *imageFile = notifier[@"profileImage"];
[self.cellImageView setImage:[UIImage imageWithData:[imageFile getData]]];
}
NSMutableString *newsText = [[NSMutableString alloc] init];
if ([eachNews[@"Type"] isEqualToString:@"Follow"]) {
[newsText appendString:[NSString stringWithFormat:@"%@ %@ %@. ", notifierString, eachNews[@"Messages"], notifiedString]];
} else if ([eachNews[@"Type"] isEqualToString:@"New Founder"]) {
[newsText appendString:[NSString stringWithFormat:@"%@ %@ %@. ", notifierString, grammer, eachNews[@"Messages"]]];
}
[newsText appendString:timeString];
NSArray *appendedString = [newsText componentsSeparatedByString:@" "];
NSRange dateRange = [newsText rangeOfString:appendedString[1]];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:newsText];
[attrString beginEditing];
[attrString addAttribute: NSForegroundColorAttributeName
value:[UIColor lightGrayColor]
range:dateRange];
[attrString endEditing];
self.cellLabel.attributedText = attrString;
self.cellLabel.numberOfLines = 0;
self.cellLabel.font = [UIFont systemFontOfSize:14];
CGSize constrainedSize = CGSizeMake(self.cellLabel.frame.size.width , CGFLOAT_MAX);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:14.0], NSFontAttributeName,
nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.cellLabel.text attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
if (requiredHeight.size.height > self.cellLabel.frame.size.height) {
requiredHeight = CGRectMake(0,0, self.cellLabel.frame.size.width, requiredHeight.size.height);
}
CGRect newFrame = self.cellLabel.frame;
newFrame.size.height = requiredHeight.size.height + 20;
self.cellLabel.frame = newFrame;
}
if (self.segmentControl.selectedSegmentIndex == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//Invites section
cell.imageView.image = nil;
cell.detailTextLabel.text = nil;
if (indexPath.section == 0) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
if (_rowOneCount == 0) {
[cell.textLabel setTextColor:[UIColor lightGrayColor]];
cell.accessoryView = nil;
} else {
[cell.textLabel setTextColor:[UIColor blackColor]];
cell.accessoryView = [MSCellAccessory accessoryWithType:FLAT_DISCLOSURE_INDICATOR color:[UIColor orangeColor]];
}
cell.textLabel.text = [NSString stringWithFormat:@"Invites (%i)", _rowOneCount];
cell.textLabel.font = [UIFont systemFontOfSize:18];
//Requested section
} else if (indexPath.section == 1){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
if (_rowTwoCount == 0) {
[cell.textLabel setTextColor:[UIColor lightGrayColor]];
cell.accessoryView = nil;
} else {
[cell.textLabel setTextColor:[UIColor blackColor]];
cell.accessoryView = [MSCellAccessory accessoryWithType:FLAT_DISCLOSURE_INDICATOR color:[UIColor orangeColor]];
}
cell.textLabel.font = [UIFont systemFontOfSize:18];
cell.textLabel.text = [NSString stringWithFormat:@"Requests (%i)", _rowTwoCount];
//Requested count is greater than 0
} else if (indexPath.section == 2) {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//Create the ImageViwew that is going into the cell
self.cellImageView = (UIImageView *)[cell.contentView viewWithTag:1];
self.cellImageView.layer.cornerRadius = 6.0f;
self.cellImageView.clipsToBounds = YES;
//Create the cell label going into the cell
self.cellLabel = (UILabel *)[cell.contentView viewWithTag:3];
[cell.contentView addSubview:self.cellLabel];
[cell.contentView addSubview:self.cellImageView];
if (self.myNews.count == 0) {
self.cellLabel.text = @"";
} else {
PFObject *myNewsObject = [self.myNews objectAtIndex:indexPath.row];
PFUser *meUser = [myNewsObject objectForKey:@"Notifier"];
[meUser fetchIfNeeded];
NSDate *timeStamp = myNewsObject.createdAt;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM d, yyyy h:mm a"];
NSString *timeString = [dateFormatter stringFromDate:timeStamp];
if (meUser[@"profileImage"] == nil) {
UIImage *hermet = [UIImage imageNamed:@"ohyeah.jpg"];
[self.cellImageView setImage:hermet];
} else {
PFFile *imageFile = meUser[@"profileImage"];
[self.cellImageView setImage:[UIImage imageWithData:[imageFile getData]]];
}
self.cellImageView.contentMode = UIViewContentModeScaleAspectFit;
NSMutableString *newsText = [[NSMutableString alloc] init];
if ([myNewsObject[@"Type"] isEqualToString:@"Follow"]) {
[newsText appendString:[NSString stringWithFormat:@"%@ %@ you. ", meUser.username, myNewsObject[@"Messages"]]];
} else if ([myNewsObject[@"Type"] isEqualToString:@"New Founder"]){
[newsText appendString:[NSString stringWithFormat:@"You are %@. ", myNewsObject[@"Messages"]] ];
}
[newsText appendString:timeString];
NSArray *appendedString = [newsText componentsSeparatedByString:@" "];
NSRange dateRange = [newsText rangeOfString:appendedString[1]];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:newsText];
[attrString beginEditing];
[attrString addAttribute: NSForegroundColorAttributeName
value:[UIColor lightGrayColor]
range:dateRange];
[attrString endEditing];
self.cellLabel.attributedText = attrString;
self.cellLabel.numberOfLines = 0;
self.cellLabel.font = [UIFont systemFontOfSize:14];
CGSize constrainedSize = CGSizeMake(self.cellLabel.frame.size.width , CGFLOAT_MAX);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:14.0], NSFontAttributeName,
nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:self.cellLabel.text attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
if (requiredHeight.size.height > self.cellLabel.frame.size.height) {
requiredHeight = CGRectMake(0,0, self.cellLabel.frame.size.width, requiredHeight.size.height);
}
CGRect newFrame = self.cellLabel.frame;
newFrame.size.height = requiredHeight.size.height + 20;
self.cellLabel.frame = newFrame;
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (self.segmentControl.selectedSegmentIndex == 2) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.accessoryView = nil;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.imageView.layer.cornerRadius = 6.0f;
cell.imageView.clipsToBounds = YES;
cell.textLabel.text = @"Some Events";
}
return cell;
}
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (self.segmentControl.selectedSegmentIndex == 1) {
NSArray *headerTitles = [[NSArray alloc] initWithObjects:@"Invites", @"Requests", @"Notifications", nil];
NSString *title = [headerTitles objectAtIndex:section];
return title;
} else if (self.segmentControl.selectedSegmentIndex == 0) {
return @"Following News";
} else {
return @"Events";
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.segmentControl.selectedSegmentIndex == 1) {
if (indexPath.section == 0) {
if (_rowOneCount == 0) {
return;
} else {
[self performSegueWithIdentifier:@"showInvites" sender:self.tableView];
}
} else if (indexPath.section == 1) {
if (_rowTwoCount == 0) {
return;
} else {
[self performSegueWithIdentifier:@"showRequests" sender:self.tableView];
}
} else if (indexPath.section == 2) {
return;
}
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.segmentControl.selectedSegmentIndex == 0) {
if (self.followingNews.count == 0) {
return 50;
} else {
PFObject *eachNews = [self.myNews objectAtIndex:indexPath.row];
PFUser *notifier = [eachNews objectForKey:@"Notifier"];
PFUser *notified = [eachNews objectForKey:@"Notified"];
[notifier fetchIfNeeded];
[notified fetchIfNeeded];
NSString *notifierString = [[NSString alloc] init];
NSString *notifiedString = [[NSString alloc] init];
if ([notifier.username isEqualToString:_loggedInUser.username]) {
notifierString = @"You";
} else {
notifierString = notifier.username;
}
if ([notified.username isEqualToString: _loggedInUser.username]) {
notifiedString = @"you";
} else {
notifiedString = notified.username;
}
NSString *fullString = [[NSString alloc] init];
if ([eachNews[@"Type"] isEqualToString:@"Follow"]) {
fullString = [NSString stringWithFormat:@"%@ %@ %@.", notifierString, eachNews[@"Messages"], notifiedString];
} else if ([eachNews[@"Type"] isEqualToString:@"New Founder"]) {
fullString = [NSString stringWithFormat:@"%@ %@.", notifierString, eachNews[@"Messages"]];
}
CGSize constrainedSize = CGSizeMake(200, CGFLOAT_MAX);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:14.0], NSFontAttributeName,
nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:fullString attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
if (requiredHeight.size.height > 45) {
requiredHeight = CGRectMake(0,0, 200, requiredHeight.size.height);
}
return requiredHeight.size.height + 50;
}
} else {
if (indexPath.section == 2) {
if (self.myNews.count == 0) {
return 50;
} else {
PFObject *myNewsObject = [self.myNews objectAtIndex:indexPath.row];
PFUser *meUser = [myNewsObject objectForKey:@"Notifier"];
[meUser fetchIfNeeded];
NSString *fullString = [[NSString alloc] init];
if ([myNewsObject[@"Type"] isEqualToString:@"Follow"]) {
fullString = [NSString stringWithFormat:@"%@ %@ you.", meUser.username, myNewsObject[@"Messages"]];
} else if ([myNewsObject[@"Type"] isEqualToString:@"New Founder"]) {
fullString = [NSString stringWithFormat:@"You are %@.", myNewsObject[@"Messages"]];
}
CGSize constrainedSize = CGSizeMake(200, CGFLOAT_MAX);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:14.0], NSFontAttributeName,
nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:fullString attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
if (requiredHeight.size.height > 45) {
requiredHeight = CGRectMake(0,0, 200, requiredHeight.size.height);
}
return requiredHeight.size.height + 50;
}
} else {
return 50;
}
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
OtherNewsViewController *otherVC = segue.destinationViewController;
if ([segue.identifier isEqualToString:@"showInvites"]) {
otherVC.segueIdentifier = @"Invites";
otherVC.navigationItem.title = @"Invites";
} else if ([segue.identifier isEqualToString:@"showRequests"]) {
otherVC.segueIdentifier = @"Requests";
otherVC.navigationItem.title = @"Requests";
}
}
@end
答案 0 :(得分:0)
没关系我找到答案/问题所在!在heightForRowAtIndexPath
方法中,我在抓取对象时传入了错误的数组
if (self.followingNews.count == 0) {
return 50;
} else {
PFObject *eachNews = [self.myNews objectAtIndex:indexPath.row];
这在IF语句的第一部分中的数组未用于PFObject的初始化。我正在使用self.myNews。我想解决问题确实需要时间!