2013-01-17 39 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方法中的數組數並在適當的位置重新加載表視圖