2013-04-25 86 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