简体   繁体   English

如何在UITableView中划分部分?

[英]How do I divide sections in UITableView?

how can i divide sections in tableview? 如何在tableview中划分部分? below is the code i tried to divide but unable to get the sections.. 下面是我试图划分但无法获取部分的代码。

#import "ipapbatch3ViewController.h"  

@implementation ipapbatch3ViewController    

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section  
{  
    NSInteger numberofrows=5;    
    return numberofrows;  

}  

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath    
{
    NSArray *a = [NSArray arrayWithObjects:@"rams",@"balu",@"praveen",@"sagar",@"faizan",nil];    
    UITableViewCell * r = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];    
    [r autorelease];  

    r.textLabel.text=[a objectAtIndex:[indexPath row]];  

                         return r;  
                         }  
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView      
{    
    NSInteger sc = 2;      
    return sc;    

}    




@end  

It's all in the cellForRowAtIndexPath, here's a small example: 全部都在cellForRowAtIndexPath中,这是一个小例子:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath    
{
    UITableViewCell * r = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];

    if (indexPath.section == 0) {
        NSArray *a = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",nil];    
        r.textLabel.text = [a objectAtIndex:indexPath.row];  

    } else {
        NSArray *b = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",nil];    
        r.textLabel.text = [b objectAtIndex:indexPath.row];  

    }

    return r;  
}

Your main problem in the code above is that you never tell which section to inser new cell, and thus they all fell into the first one. 上面代码中的主要问题是,您从不告诉要插入新单元格的部分,因此它们都属于第一个部分。 Look into the method cellForRowAtIndexPath you need to specify that the newly created cell goes to a particular section. 查看方法cellForRowAtIndexPath,您需要指定新创建的单元格转到特定部分。

Go through those example programs provided by apple. 查看apple提供的示例程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM