UITableview 可滚动问题

问题描述:

我正在将所有数据添加到我的 iPhone 应用程序中,除了将数据硬编码到其中之外,还完成了编码.我遇到了一个问题.我正在使用 UITableviews 并且我在每个表视图中添加了大约 150-250 个项目,它允许我添加数据,但是当我运行它时,它不会让我向下滚动超过大约 12 个项目.我希望能够滚动浏览所有 200 多个项目,但正如我所说,它只允许前 12 个左右.我将在下面发布我的代码片段.

I am in the process of adding all of my data to my iPhone application, the coding is done other than hard coding the data into it. I have run into an issue. I am using UITableviews and I am adding around 150-250 items per table view, it allows me to add the data, but when I run it won't let me scroll down past about 12 items. I would like to be able to scroll through all 200+ of my items, but as I said it only allows the first 12 or so. I will post a snippet of my code below.

RootTableViewController.h

#import <UIKit/UIKit.h>

@interface RootTableViewController : UITableViewController

@end

RootTableViewController.m

#import "RootTableViewController.h"
#import "SecondTableViewController.h"

@interface RootTableViewController ()

@end

@implementation RootTableViewController
{
NSArray *states;
}

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

states = [NSArray arrayWithObjects:@"Alabama", @"Georgia", @"Tennessee", @"Colorado", nil];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


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

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


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//table identifier
static NSString *simpleTableIdentifier = @"StateCell";

//creating a cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

//if cell doesn't have anything in it, creates a new one
if(cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

//creates text for cell, depending on what row it is
cell.textLabel.text = [states objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont fontWithName:@"Chalkduster" size:17];

return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//push segue identifier 'showArrayDetail'
if([segue.identifier isEqualToString:@"showStateDetail"])
{
    //row that we clicked on
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

    //'SecondTableVieController' object is created
    SecondTableViewController *destViewController = segue.destinationViewController;

    //sets 'stateName' to what row you pick
    destViewController.stateName = [states objectAtIndex:indexPath.row];

    //sets title to 'stateName' you picked
    destViewController.title = [NSString stringWithFormat:@"%@ Areas", destViewController.stateName];
}
}

@end

SecondTableViewController.h

#import <UIKit/UIKit.h>

@interface SecondTableViewController : UITableViewController

@property (nonatomic, strong) NSString *stateName;

@end

SecondTableViewController.m

#import "SecondTableViewController.h"
#import "ThirdTableViewController.h"

@interface SecondTableViewController ()

@end

@implementation SecondTableViewController
{
NSArray *areas;
}

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

//populating arrays
NSDictionary *dict = @{@"Alabama":@[@"Moss Rock Preserve Boulder Fields", @"Alabama Area 2", 
@"Alabama Area 3"], @"Georgia": @[@"Georgia Area 1", @"Georgia Area 2", @"Georgia Area 3"], 
@"Tennessee":@[@"Tennessee Area 1", @"Tennessee Area 2", @"Tennessee Area 3"], 
@"Colorado":@[@"Colorado Area 1", @"Colorado Area 2", @"Colorado Area 3"]};

areas = dict[self.stateName];



}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return areas.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"AreaCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.textLabel.text = areas[indexPath.row];
return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//push segue identifier 'showArrayDetail'
if([segue.identifier isEqualToString:@"showAreaDetail"])
{
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    ThirdTableViewController *destViewController = segue.destinationViewController;
    destViewController.areaName = areas[indexPath.row];
    destViewController.title = [NSString stringWithFormat:@"%@ Climbs", destViewController.areaName];
}
}

@end

ThirdTableViewController.h

#import <UIKit/UIKit.h>

@interface ThirdTableViewController : UITableViewController

@property (nonatomic, strong) NSString *areaName;

@end

ThirdTableViewController.m

#import "ThirdTableViewController.h"
#import "FourthTableViewController.h"

@interface ThirdTableViewController ()

@end

@implementation ThirdTableViewController
{
NSArray *climbs;
}

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

//populating arrays
NSDictionary *dict = @{@"Moss Rock Preserve Boulder Fields":@[@"Tesseract", @"Chalky Dreams",   
@"Aristocratic Nose", @"Bee Stings", @"Recovery Run Traverse", @"Heel Shock", @"Fourth of July", 
@"No Sack", @"Poop Dreams", @"Hoop Dreams", @"Grass Man Traverse", @"Mikey Likes It", @"Just 
Throw", @"Rapture"], @"Georgia Area 1": @[@"Georgia Climb 1", @"Georgia Climb 2", @"Georgia 
Climb 3"], @"Tennessee Area 1":@[@"Tennessee Climb 1", @"Tennessee Climb 2", @"Tennessee Climb 
3"], @"Colorado Area 1":@[@"Colorado Climb 1", @"Colorado Climb 2", @"Colorado Climb 3"]};

climbs = dict[self.areaName];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return climbs.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ClimbCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.textLabel.text = climbs[indexPath.row];
return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 //push segue identifier 'showArrayDetail'
 if([segue.identifier isEqualToString:@"showClimbDetail"])
 {
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
     FourthTableViewController *destViewController = segue.destinationViewController;
     destViewController.climbName = climbs[indexPath.row];
     destViewController.title = [NSString stringWithFormat:@"%@ Specs", destViewController.climbName];
 }

 }

@end

FourthViewController.h

#import <UIKit/UIKit.h>

@interface FourthTableViewController : UITableViewController

@property (nonatomic, strong) NSString *climbName;

@end

FourthViewController.m

#import "FourthTableViewController.h"

@interface FourthTableViewController ()

@end

@implementation FourthTableViewController
{
NSArray *specs;
}

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

//populating arrays
NSDictionary *dict = @{@"Tesseract":@[@"Alabama Spec 1", @"Alabama Spec 2", @"Alabama Spec 3"], 
@"Georgia Climb 1": @[@"Georgia Spec 1", @"Georgia Spec 2", @"Georgia Spec 3"], @"Tennessee 
Climb 1":@[@"Tennessee Spec 1", @"Tennessee Spec 2", @"Tennessee Spec 3"], @"Colorado Climb 
1":@[@"Colorado Spec 1", @"Colorado Spec 2", @"Colorado Spec 3"]};

specs = dict[self.climbName];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return specs.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SpecCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.textLabel.text = specs[indexPath.row];
cell.textLabel.font = [UIFont fontWithName:@"Chalkduster" size:17];

return cell;
}

我想我需要像在 Java 应用程序中一样添加一个可滚动部分",但它已经是可滚动的....

I was thinking I needed to add a "scrollable section" like I would in a Java application, but it is already scrollable....

谢谢

好的,我把正确的评论移到这里.

Okay, so I move the correct comment to here.

当你到达桌子的尽头时,你仍然可以把它拉长一点,对吗?当你这样做时,是否有任何单元格有数据但隐藏在 tableView 的框架之外?如果是,请检查tableView的frame.

When you reach to the end of the table, you can still stretch it up a little bit right? When you do that, is there any cell that has data but was hidden outside the frame of the tableView? If so, please check the frame of the tableView.