IOS UITableView Group&Section

UItableView 根据数据结构不同 会有不同样式 关键在两个代理 tableviewdelegate&tabledatasourse

下面代码是我实施的Group 在模拟器中 ios6.1和ios7

并且滚动后相应的section会“置顶”,效果不错哦!

核心代码:

复制代码
#import <UIKit/UIKit.h>

@interface AnnouncementViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *tableView_Inform;
}

@end
//////////////////////////////////////////////////////

- (void)viewDidLoad
{
    [self.view addSubview: [NavView NavView_Title:@"通告"]];
    [super viewDidLoad];
    [self TableView];
}
-(void)TableView
{
    if(StatusBar_System>0)
    {
        moment_status_bar=20;
    }
    tableView_Inform=[[UITableView alloc]initWithFrame:CGRectMake(0, 44+moment_status_bar, 320,Phone_Height-64-44 )];
    [self.view addSubview:tableView_Inform];
    tableView_Inform.dataSource=self;
    tableView_Inform.delegate=self;
}
/*
 ios7以前的group 一般是圆角的,ios7以后没有圆角 苹果鼓励并支持更大的阅读面积 所以我把ios6的圆角也去掉了 也挺帅的 不是么
*/
//每一个section中间的宽度 可以根据需要设定 如果第一个section不需要宽度 就return 0;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (!section == 0) {
        
        return 44.0;
    } else {
        return 44.0;
    }
}
//一共有几组 返回有几个 section 块
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
//每组有几行  返回 每个块 有几行cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSArray *array=[NSArray arrayWithObjects:@"One",@"Two", @"Three",@"Four",nil];
    return [array objectAtIndex:section];
}
//cell注意数据结构的变化 indexPath.row 是循环变化的
//这里cell 我是写死的 可以自定义哦哦
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: SimpleTableIdentifier] ; } NSLog(@"%d",indexPath.row); return cell; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
复制代码

附加 ios6和ios7的两个效果图 

posted on   ACM_Someone like you  阅读(682)  评论(0编辑  收藏  举报

编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示