【本文來自blog.csdn.net/lanmanck】
建立TableViewController和關聯的類就不說了。
要顯示Cell資料,做如下幾步驟:
1、在Storyboard點選單個Cell,在Attributor Inspector的Identifier設定好,Accessory也設定好,例如設為Disclosure Indicator
2、在ApplicationDelegate.m裡實作資料源,如果TableView有多個Section就搞多個資料源,參考部落格:
iOS Storyboard 初探(三)
3、在TableViewController的實作檔案中重載TableView的函數,解析如下:
設定Section個數:
//傳回tableview的章節數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 2;
}
設定每個Section的Cell數:
//傳回數組個數,指定顯示多少cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
//根據不同的section數值傳回不同的count
if(section == 1)
return [self.playersArray2 count];
else
return [self.playersArray count];
}
設定要顯示的資料:
//indexPath标示的是你要渲染的這個cell的位置,indexPath對象裡有兩個屬性,section和row,顧名思義,可以定位某個cell。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if(indexPath.section == 0){
//這個比對的字元串要在attributor inspector中的accessory設定
cell = [tableView dequeueReusableCellWithIdentifier:@"PlayerCell"];
// Configure the cell...
if(cell !=nil){
DGPlayer *player = [self.playersArray objectAtIndex:indexPath.row]; //找到數組下标
cell.textLabel.text = player.name; //指派給目前的cell
cell.detailTextLabel.text = player.game;
}
}else{
//這個比對的字元串要在attributor inspector中的accessory設定
cell = [tableView dequeueReusableCellWithIdentifier:@"PlayerCell2"];
// Configure the cell...
if(cell !=nil){
DGPlayer *player = [self.playersArray2 objectAtIndex:indexPath.row]; //找到數組下标
cell.textLabel.text = player.name; //指派給目前的cell
cell.detailTextLabel.text = player.game;
}
}
return cell;
}
4、何為Section和Cell?看下這個連結,圖文并茂:
http://segmentfault.com/q/1010000000095547