2013-04-25 87 views
1

有人可以請用基本術語解釋添加節的流程是如何工作的?如何將部分添加到UITableView?

我有一個對象數組,我目前正在填充到一個單獨的部分UITableView,但是我想根據這些對象的共享「類別」屬性將它們分成多個部分。我從API獲取對象列表,所以我不知道每個類別中我會有多少。

很多,很多預先感謝。

回答

1

你必須使用UITableViewDataSource協議。你要實現的可選方法之一:

//Optional 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Default is 1 if not implemented 

    NSUInteger sectionCount = 5; //Or whatever your sections are counted as... 
    return sectionCount; 
} 

確保那麼你的行計數爲每個板塊:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 4; //This will return 4 rows in each section 
} 

如果你希望把你的頁眉和頁腳爲每個板塊:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return @""; 
}// fixed font style. use custom view (UILabel) if you want something different 

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section 
{ 
    return @""; 
} 

最後,確保你正確地貫徹細胞:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier: 
    // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) 

    NSUInteger section = [indexPath indexAtPosition:0]; 
    NSUInteger rowInSection = [indexPath indexAtPosition:1]; 

    //Do your logic goodness here grabbing the data and creating a reusable cell 

    return nil; //We would make a cell and return it here, which corresponds to that section and row. 
} 

可摺疊的部分是一個完整的其他野獸。你需要繼承UITableView,或者在CocoaControls上找到它。

+0

謝謝發佈!如何指定單元格在返回之前轉到的部分? – bruchowski 2013-04-25 03:41:27

+0

tableview:cellForRowAtIndexPath:基本上要求您爲表格視圖的x行y中的單元格。請注意,我是如何將索引路徑拉出並排出的。 – Derek 2013-04-25 03:59:26