您需要設置的UITableViewDelegate和UITableViewDataSource您的所有表格,將實現一類以下方法:
對於UITableViewDataSource:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
和
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
因爲這些方法通過調用tableview,你應該很容易返回正確的數據爲有問題的tableview(你已經是stori ng在NSMutableArray中)。你仍然需要爲陣列顯示不同的內容,但我相信你會設法做到這一點。 NSIndexPath基本上告訴你應該顯示數組的哪一部分。假設,現在,你是在一個未分組表的工作,你會簡單的創建一個新的細胞,並用你的陣列,它是由indexPath確定的內容填充:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"MyNiceIdentifier";
cell = [aTableView dequeueReusableCellWithIdentifier:NavigationCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.tag = 500;
}
cell.textLabel = [myArray objectAtIndex:indexPath.row];
}
中的其它數據源方法,你簡單的返回了該陣列計數:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(tableView == myFirstTableView) {
return [myFirstArray count];
}
}
的UITableViewDelegate的你可能會經常使用的方法是這樣的:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
您可以使用它的回報給定indexPath處單元的所需高度。
設置數據源和委託是簡單地做:
myTableView.delegate = ...
myTableView.dataSource = ...
看到這個文檔:http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDelegate
請同時參閱本文檔: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
謝謝你的答覆。但是我不知道如何爲每個表實現這個。我試過了,第二張桌子模仿了第一張桌子。 「*」是什麼意思?我很感激幫助。 – motoxrider365 2011-02-05 18:51:50