我想問一個關於目標C的UITableView的問題。我正在編寫一個程序,我想以編程方式創建UI。但是,我不知道如何以編程方式顯示錶格。我已經有一個NSMutableArray來存儲顯示的數據。我創建了一個對象UITableView *tableData;
,下一步應該怎麼做?非常感謝你。如何以編程方式顯示UITableView?
回答
寫完上面的代碼後,你必須實現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;
}
像這樣的東西應該做的伎倆(假設這是從您的視圖控制器內運行,你有一個屬性設置爲表視圖):如果你與任何位置/大小更換CGRectMake(...)
tableView = [[[UITableView alloc] initWithFrame:CGRectMake(...) style:UITableViewStylePlain] autorelease];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
你想。
感謝您的回覆。但我想問一下,我應該在哪裏放置NSMutableArray?謝謝。 – Questions 2010-07-26 09:31:18
您可以將數組存儲在您的視圖控制器中,並在UITableViewDataSource方法中使用它。例如,您將返回tableView:numberOfRowsInSection:中的數組長度。 – 2010-07-26 09:46:30
感謝您的回覆。由於UI包含其他元素,因此我將UITableView添加爲其中的一部分。我不知道如何調用UITableDataSource,因爲在創建它時類不是表視圖類。你介意告訴我如何導入或添加數組中的數據?謝謝。 – Questions 2010-07-26 09:59:07
- 創建從繼承的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
也許它也爲大家誰在新的有用的那裏。
- (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
爲新人(我),複製/粘貼到通用應用程序,更改'JSCustomCell'爲'UITableViewCell',更改'cell.descriptionLabel.text'爲'cell.textLabel.text'。返回'numberOfRowsInSection'的數組項的數量。提醒在.h文件中聲明委託方法(我認爲這就是所謂的),'
- 1. 如何在Objective-C中以編程方式隱藏/顯示UITableView?
- 2. 以編程方式突出顯示UITableView單元格
- 3. 以編程方式顯示桌面
- 4. 顯示進度條 - 以編程方式
- 5. 以編程方式顯示主視圖
- 6. 以編程方式顯示iPhone鍵盤
- 7. 以編程方式突出顯示UIBarButtonItem
- 8. 以編程方式顯示dijit /菜單
- 9. 以編程方式顯示RowDetails
- 10. 以編程方式顯示options.xul
- 11. 如何以編程方式輸入目標顯示模式?
- 12. 如何以編程方式調用UITableView的編輯屬性
- 13. 以編程方式向UIViewController添加UITableView
- 14. 以編程方式創建UITableView
- 15. UITableView以編程方式選擇選項
- 16. 以編程方式創建uitableview
- 17. 如何以編程方式顯示ViewFlipper的第二個孩子?
- 18. 如何以編程方式在Android中顯示推文Feed?
- 19. 如何以編程方式顯示錶情符號鍵盤android?
- 20. 如何讓標題欄在NSWindow中以編程方式顯示?
- 21. 如何以編程方式顯示圖像
- 22. 如何在Android中以編程方式顯示/隱藏windowActionBar?
- 23. 如何以編程方式顯示mdl-textfield的錯誤消息?
- 24. 你將如何以編程方式確定顯示哪個UITableViewRow?
- 25. 如何獲取MyController以編程方式顯示url?
- 26. 如何在Firefox OS中以編程方式顯示鍵盤?
- 27. 如何以編程方式隱藏Android上的來電顯示
- 28. 如何以編程方式顯示鍵盤
- 29. 如何以編程方式顯示/隱藏ExtJS.Toolbar按鈕
- 30. 如何以編程方式顯示WPF/C#Windows.Control.ToolTip?
感謝您的回覆。但是我沒有使用UIViewTable類,我只是使用簡單的類。你介意告訴我如何將數組添加到表格或表格單元格嗎?謝謝。 – Questions 2010-07-26 10:03:56
@MarkSiu沒有必要實現UITableView類只需編寫代碼在你的viewdidload函數中添加UITableView,並在你的視圖 – priyanka 2010-07-26 11:42:43
感謝您的回覆。我在.m類中添加了上述函數。但是,我發現這些函數沒有被UITableView調用。何時調用函數?謝謝。 – Questions 2010-07-27 01:02:55