0

似乎我的表格部分加载,其余部分在我上下滚动几次后加载。毫无疑问,这与我[[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
4

1 回答 1

5

在 tableView cellForRowAtIndexPath: 方法中,像这样更改代码,(你的 else 部分不正确)

    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];
于 2013-04-30T02:39:10.243 回答