为什么我的UITableView没有完全加载单元格内容?

时间:2013-04-30 02:12:55

标签: ios objective-c uitableview

似乎我的表部分加载,其余的加载在我上下滚动几次之后。毫无疑问,这与我的[[self myTableView] reloadData];以及-(void)viewWillAppear:(BOOL)animated,-(void)viewDidAppear:(BOOL)animated,-(void)viewDidLoad之间的位置有关,尽管我无法将其放在上面。在这种情况下,没有要求提供数据;我只是尝试在应用启动时立即加载所有指定的数据。

#import "PreViewController.h"
#import <CoreData/CoreData.h>
#import <CoreLocation/CoreLocation.h>
#import "FlipsideViewController.h"
#import "AppDelegate.h"
#import <SystemConfiguration/SystemConfiguration.h>
#import "Reachability.h"

@interface PreViewController ()

{
    NSMutableArray *arrayNo;
}

@end

@implementation PreViewController

-(void)viewWillAppear:(BOOL)animated
{
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkForReachability) name:kReachabilityChangedNotification object:nil];

    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if(remoteHostStatus == NotReachable)
    {
        UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle: @"Please check your network connection and try again."
                          message: @""
                          delegate: nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
        [alert show];
    }
 }

-(void)viewDidAppear:(BOOL)animated
{    

   NSString *arts = [NSString stringWithFormat:@"Arts and Museums"];
   NSString *coffee = [NSString stringWithFormat:@"Coffee and Bakeries"];
   NSString *tours = [NSString stringWithFormat:@"Tours and Festivals"];
   NSString *hotels = [NSString stringWithFormat:@"Hotels and Inns"];
   NSString *leisure = [NSString stringWithFormat:@"Leisure and Recreation"];
   NSString *music = [NSString stringWithFormat:@"Live Music"];
   NSString *bars = [NSString stringWithFormat:@"Night Clubs and Bars"];
   NSString *food = [NSString stringWithFormat:@"Restaurants"];
   NSString *shopping = [NSString stringWithFormat:@"Shopping"];
   NSString *transportation = [NSString stringWithFormat:@"Transportation"];

   [arrayNo addObject:arts];
   [arrayNo addObject:coffee];
   [arrayNo addObject:tours];
   [arrayNo addObject:hotels];
   [arrayNo addObject:leisure];
   [arrayNo addObject:music];
   [arrayNo addObject:bars];
   [arrayNo addObject:food];
   [arrayNo addObject:shopping];
   [arrayNo addObject:transportation];

   [[self myTableView] reloadData];
}

- (void)viewDidLoad
{
   [super viewDidLoad];
   arrayNo = [[NSMutableArray alloc] init];

   [[self myTableView] setDelegate:self];
   [[self myTableView] setDataSource:self];
}


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

}

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

- (UIFont *)fontForCell
{
   return [UIFont boldSystemFontOfSize:14.0];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  static NSString *CellIdentifier = @"Cell";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (!cell)
  {
      NSLog(@"CREATING NEW CELL");
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  }
  else
  {
      cell.textLabel.text = [arrayNo objectAtIndex:indexPath.row];
      cell.textLabel.textColor = [UIColor whiteColor];
      cell.textLabel.textAlignment = NSTextAlignmentCenter;
      cell.textLabel.textColor = [UIColor colorWithRed:(100/255.0) green:(130/255.0) blue:(255/255.0) alpha:1.0];
  }
  return cell;
}


- (void)didReceiveMemoryWarning
{
  [super didReceiveMemoryWarning];
}

@end

1 个答案:

答案 0 :(得分:5)

在tableView cellForRowAtIndexPath:方法中的

,更改这样的代码,(你的其他部分不正确)

    if (!cell)
      {
         NSLog(@"CREATING NEW CELL");
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
         cell.textLabel.textColor = [UIColor whiteColor];
         cell.textLabel.textAlignment = NSTextAlignmentCenter;
         cell.textLabel.textColor = [UIColor colorWithRed:(100/255.0) green:(130/255.0) blue:(255/255.0) alpha:1.0];
      }

  cell.textLabel.text = [arrayNo objectAtIndex:indexPath.row];