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

标签 ios objective-c uitableview

我的表格似乎加载了一部分,其余部分在我上下滚动几次后加载。毫无疑问,这与我的 [[self myTableView] reloadData]; 以及我在 -(void)viewWillAppear:(BOOL)animated,-(void)viewDidAppear:(BOOL) 之间的位置有关animated,-(void)viewDidLoad 虽然我不能 Handlebars 指放在上面。在这种情况下没有数据请求;我只是想在应用程序启动时立即加载所有指定的数据。

#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

最佳答案

在 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];

关于ios - 为什么我的 UITableView 没有完全加载单元格内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16291151/

相关文章:

iphone - 如何使此图像选择器在iPad上工作?

ios - 将协议(protocol)转换为方法参数并传递

objective-c - 绘制矩形看到空数组

ios - Nib 必须恰好包含一个顶级对象,该对象必须是 UITableViewCell 实例

ios - 在 Podfile 中包含文件

ios - 如何在DynamoDB(iOS)中查询StringSet值?

ios - UITableViewCell 的 accessoryView 中的多个图像

objective-c - 填充餐 table 带有firebase中的图像

iphone - 项目构建没有错误但未运行

ios - 如何消除文本字段中某个字符的出现并使其仅出现在前缀处