2010-07-26 113 views
22

我想問一個關於目標C的UITableView的問題。我正在編寫一個程序,我想以編程方式創建UI。但是,我不知道如何以編程方式顯示錶格。我已經有一個NSMutableArray來存儲顯示的數據。我創建了一個對象UITableView *tableData;,下一步應該怎麼做?非常感謝你。如何以編程方式顯示UITableView?

回答

38

寫完上面的代碼後,你必須實現UITableView的委託方法。

這些都是UITableView的負載的委託方法

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    } 

    // Configure the cell... 
    cell.textLabel.text = [yourarray objectAtIndex:indexPath.row]; 

    return cell; 

} 
+0

感謝您的回覆。但是我沒有使用UIViewTable類,我只是使用簡單的類。你介意告訴我如何將數組添加到表格或表格單元格嗎?謝謝。 – Questions 2010-07-26 10:03:56

+0

@MarkSiu沒有必要實現UITableView類只需編寫代碼在你的viewdidload函數中添加UITableView,並在你的視圖 – priyanka 2010-07-26 11:42:43

+0

感謝您的回覆。我在.m類中添加了上述函數。但是,我發現這些函數沒有被UITableView調用。何時調用函數?謝謝。 – Questions 2010-07-27 01:02:55

43

像這樣的東西應該做的伎倆(假設這是從您的視圖控制器內運行,你有一個屬性設置爲表視圖):如果你與任何位置/大小更換CGRectMake(...)

tableView = [[[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStylePlain] autorelease]; 
tableView.dataSource = self; 
tableView.delegate = self; 

[self.view addSubview:tableView]; 

你想。

+0

感謝您的回覆。但我想問一下,我應該在哪裏放置NSMutableArray?謝謝。 – Questions 2010-07-26 09:31:18

+0

您可以將數組存儲在您的視圖控制器中,並在UITableViewDataSource方法中使用它。例如,您將返回tableView:numberOfRowsInSection:中的數組長度。 – 2010-07-26 09:46:30

+0

感謝您的回覆。由於UI包含其他元素,因此我將UITableView添加爲其中的一部分。我不知道如何調用UITableDataSource,因爲在創建它時類不是表視圖類。你介意告訴我如何導入或添加數組中的數據?謝謝。 – Questions 2010-07-26 09:59:07

21
  • 創建從繼承的UIViewController一個新的類。
  • 使其符合UITableViewDataSource協議。
  • 聲明您的表格視圖。

你的頭文件應該是這樣的:

@interface MyViewController : UIViewController <UITableViewDataSource> {  

} 

@property (nonatomic, retain) UITableView *tableView; 

@end 

在你的類的viewLoad方法:

  • 創建使用initWithFrame表視圖。在整個高度上使用尺寸320x460。如果您有導航欄,請從高度刪除44;如果您有導航欄,請刪除44。
  • 創建一個新視圖。
  • 將表格視圖添加到新視圖。
  • 將控制器視圖設置爲新視圖。
  • 將表視圖數據源設置爲您的實例(self)。
  • 實現兩個必需的數據源方法:的tableView:的cellForRowAtIndexPath和的tableView:numberOfRowsInSection

你實現文件應該是這樣的:

#import "MyViewController.h" 

@implementation MyViewController 

@synthesize tableView=_tableView; 

- (void)dealloc 
{ 
    [_tableView release]; 

    [super dealloc]; 
} 

#pragma mark - View lifecycle 

- (void)loadView 
{ 
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460.0) style:UITableViewStylePlain]; 
    self.tableView = tableView; 
    [tableView release];  

    UIView *view = [[UIView alloc] init]; 
    [view addSubview:self.tableView]; 
    self.view = view; 
    [view release]; 

    self.tableView.dataSource = self; 
} 

- (void)viewDidUnload { 
    self.tableView = nil; 

    [super viewDidUnload]; 
} 

#pragma mark - Table view data source 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *MyCellIdentifier = @"MyCellIdentifier"; 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyCellIdentifier]; 

    if(cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyCellIdentifier] autorelease]; 
    } 

    return cell; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 5; 
} 

@end 
2

也許它也爲大家誰在新的有用的那裏。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // init table view 
    tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; 

    //or, you may do that 
    //tableView = [[UITableView alloc] init]; 
    //tableView.frame = CGRectMake:(5 , 5 , 320 , 300); 

    // must set delegate & dataSource, otherwise the the table will be empty and not responsive 
    tableView.delegate = self; 
    tableView.dataSource = self; 

    tableView.backgroundColor = [UIColor cyanColor]; 

    // add to canvas 
    [self.view addSubview:tableView]; 
} 

#pragma mark - UITableViewDataSource 
// number of section(s), now I assume there is only 1 section 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)theTableView 
{ 
    return 1; 
} 

// number of row in the section, I assume there is only 1 row 
- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 1; 
} 

// the cell will be returned to the tableView 
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"HistoryCell"; 

    // Similar to UITableViewCell, but 
    JSCustomCell *cell = (JSCustomCell *)[theTableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     cell = [[JSCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 
    // Just want to test, so I hardcode the data 
    cell.descriptionLabel.text = @"Testing"; 

    return cell; 
} 

@end 
+0

爲新人(我),複製/粘貼到通用應用程序,更改'JSCustomCell'爲'UITableViewCell',更改'cell.descriptionLabel.text'爲'cell.textLabel.text'。返回'numberOfRowsInSection'的數組項的數量。提醒在.h文件中聲明委託方法(我認爲這就是所謂的),''。 (謝謝Abhay) – tmr 2015-04-06 23:06:17

相關問題