2010-12-06 186 views

回答

2

您必須首先在界面文件中使用UITableView的IBOutlet對象,然後使用界面構建器將其綁定。在界面構建器屬性中設置UITableView的委託和數據源。

並在您的項目中執行以下編碼。

在h文件:

@interface RootViewController : UITableViewController<UITableViewDelegate,UITableViewDataSource> { 

    IBOutlet UITableView *tblView; 
} 

在.m文件:

// Customize the number of sections in the table view. 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 

//自定義表中的視圖中的行的數目。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return n;// n for number of row. 
} 

//自定義表格視圖單元格的外觀。

- (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. 

    return cell; 
}