繁体   English   中英

如何在UITableView中划分部分?

[英]How do I divide sections in UITableView?

如何在tableview中划分部分? 下面是我试图划分但无法获取部分的代码。

#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  

全部都在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;  
}

上面代码中的主要问题是,您从不告诉要插入新单元格的部分,因此它们都属于第一个部分。 查看方法cellForRowAtIndexPath,您需要指定新创建的单元格转到特定部分。

查看apple提供的示例程序。

暂无
暂无

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

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