2013-01-17 37 views
0

我对Objective-C非常陌生。我刚刚搜索并试图了解UITableViewUITableView for iOS

我已经创建了这个UITableView。而不是所有行中的"a",我希望它的顺序如"a b c d...",如果我增加no行数,它应该滚动。它不滚动,所以这里是我的代码。

#import "ViewController.h" 
@interface ViewController() 
@end 
@implementation ViewController 
- (void)viewDidLoad 
{ 
    CGRect frame = self.view.frame; 
    UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain]; 
    tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
    tableView.backgroundColor = [UIColor cyanColor]; 
    tableView.delegate = self; 
    tableView.dataSource = self; 
    [self.view addSubview:tableView]; 
    [super viewDidLoad]; 
} 
- (void)viewDidUnload 
{ 
[super viewDidUnload]; 
} 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return 20; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIDent"]; 
    cell.textLabel.text = @"a"; 
    return cell; 
} 
@end 
+0

您需要浏览一些iOS教程。检查http://www.raywenderlich.com/tutorials。还有'[super viewDidLoad];'应该是方法中的第一个调用。 – iDev

+0

查看苹果公司的参考@ http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html –

+0

我感谢您的兴趣,学习单元格索引行将返回单元格一个为了所以这里甚至第二个单元格返回一个。尝试改变该逻辑和滚动看到下面的答案,他们是美丽的框架。 –

回答

0

:添加这些行

char ch = 'a' + indexPath.row; 
cell.textLabel.text = [NSString stringWithFormat:@"%c" , ch]; 
+0

也在你的viewDidLoad [tableView setScrollEnabled:YES]; –

+0

谢谢。它为索引工作,但屏幕仍然不滚动。 – Newbee

+0

将这两行添加到viewDidLoad的末尾,我相信你会得到它的工作 [tableView setScrollEnabled:YES]; [tableView setUserInteractionEnabled:YES]; –

0

检查本教程中,我相信它会帮助你

UItableView Guide

和滚动,你不需要做任何事情自己,为以后的细胞数量的增加提供的尺寸,它会自动变为滚动,确保滚动在您UITableView属性检查器中启用,检查图像

enter image description here

快乐编码

问候

在你的cellForRowAtIndexPath
0

使用下面的代码.....在定义alphabetarr全球然后在代码中使用它......

- (void)viewDidLoad 
{ 
    alphabetarr = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j",@"k",@"l",@"m",@"n",@"o",@"p",@"q",@"r",@"s",@"t",@"u",@"v",@"w",@"x",@"y",@"z", nil]; 

    CGRect frame = self.view.frame; 
    UITableView *tableview = [[UITableView alloc] initWithFrame:frame]; 
    tableview.backgroundColor = [UIColor cyanColor]; 
    tableview.separatorColor = [UIColor blackColor]; 
    tableview.delegate = self; 
    tableview.dataSource = self; 
    [self.view addSubview:tableview]; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [alphabetarr count]; 
} 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 30.0 ; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell!=nil) { 
     cell = nil; 
    } 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

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


    return cell; 
} 
1

设置表视图代理IB或如果你创建编程然后在viewDidLoad以一个数组填充它动态或静态.Give在NumberOfRowsInsection方法中的数组数并在适当的位置重新加载表视图