我正在將我的所有數據添加到我的iPhone應用程序中,編碼完成的不是將數據硬編碼到其中。我遇到了一個問題。我使用的是UITableviews,我在每個表格視圖中添加了大約150-250個項目,它允許我添加數據,但是當我運行它時,不會讓我向下滾動大約12個項目。我希望能夠瀏覽所有200多個我的項目,但正如我所說,它只允許前12個左右。我將在下面發佈我的代碼片段。UITableview可滾動問題
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應用程序,但它已經是滾動....
謝謝
你用什麼數組/字典填充tableView?如果在這種情況下使用'爬升',則只能獲得大約3個項目。請提供您的代碼爲'tableView:cellForRowAtIndexPath'和'tableView:numberOfRowsInSection' – Emil
@Emil對不起,剛剛添加了我的代碼的其餘部分,應該更好地顯示我的問題 – javaGeek
self.areaName的內容是什麼? – J2theC