IOS UI 第九篇: UITABLEVIEW
{
NSMutableArray *dataSourse;
}
for (int i=0; i<50; ++i) {
NSString *str = [NSString stringWithFormat:@"the %d row", i];
[dataSourse addObject:str];
}
for (int s=0; s<5; ++s) {
NSMutableArray *sectionArray=[NSMutableArray array];
for (int r=0; r<10; ++r) {
NSString *str=[NSString stringWithFormat:@"the %d group--the %d row", s, r];
[sectionArray addObject:str];
}
[dataSource addObject:sectionArray];
}
myTableVie.delegate = self;
myTableVie.dataSource = self;
[self.view addSubview:myTableVie];
@interface ViewController ()<UITableViewDataSource , UITableViewDelegate]]>
@end
{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"xxxx"];
NSString *str = dataSourse[indexPath.row];
cell.textLabel.text=[NSString stringWithFormat:@"I am a cell %@", str];
return cell;
}
{
return dataSourse.count;
}
{
static NSString *reuseID = @"myCell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuseID];
if (cell == nil) {
NSLog(@"alloc new cell");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseID];
cell.detailTextLabel.text = [NSString stringWithFormat:@"previous %d", indexPath.row];
}
NSString *str=dataSourse[indexPath.row];
cell.textLabel.text=[NSString stringWithFormat:@"I am a cell %@", str];
return cell;
}
{
[super viewDidLoad];
dataSource = [NSMutableArray array];
for (int s=0; s<5; ++s) {
NSMutableArray *sectionArray=[NSMutableArray array];
for (int r=0; r<10; ++r) {
NSString *str=[NSString stringWithFormat:@"the %d group--the %d row", s, r];
[sectionArray addObject:str];
}
[dataSource addObject:sectionArray];
}
UITableView *myTableVie = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 320, self.view.frame.size.height) style:UITableViewStylePlain];
myTableVie.delegate = self;
myTableVie.dataSource = self;
UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
headView.backgroundColor=[UIColor redColor];
myTableVie.tableHeaderView=headView;
[self.view addSubview:myTableVie];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [NSString stringWithFormat:@"The %d group", section];
}
//DIY group header , but if overwrite the function, the groupheader function is unvaliable..
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
headView.backgroundColor = [UIColor blackColor];
UILabel *label=[[UILabel alloc] initWithFrame:headView.frame];
label.text=[NSString stringWithFormat:@"The %d group", section];
label.textColor=[UIColor redColor];
label.textAlignment = NSTextAlignmentCenter;
label.font=[UIFont boldSystemFontOfSize:15];
[headView addSubview:label];
return headView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return dataSource.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *sectionArray=dataSource[section];
return sectionArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseID = @"myCell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuseID];
if (cell == nil) {
NSLog(@"alloc new cell");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseID];
cell.detailTextLabel.text = [NSString stringWithFormat:@"previous %d", indexPath.row];
}
NSString *str=dataSource[indexPath.section][indexPath.row];
cell.textLabel.text=str;
return cell;
}
NSLog(@"tableview ");
//delete the data in a row
NSMutableArray *sectionArray = dataSource[indexPath.section];
[sectionArray removeObjectAtIndex:indexPath.row];
//delete a row in tableview
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
NSLog(@"d1 :%@", sender);
NSLog(@"d2 :%@", self);
myTableView.editing=NO;
}else{
myTableView.editing=YES;
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return dataSource.count;
}
//Set group header height
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
//Set group header text
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [NSString stringWithFormat:@"The %d group",section];
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
UILabel *label=[[UILabel alloc]initWithFrame:headView.frame];
label.text=[NSString stringWithFormat:@"The %d group ",section];
label.font=[UIFont boldSystemFontOfSize:15];
label.textColor=[UIColor redColor];
label.textAlignment=NSTextAlignmentCenter;
[headView addSubview:label];
return headView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *sectionArray=dataSource[section];
return sectionArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *reuseID=@"myCell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuseID];
if (cell==nil) {
NSLog(@"new cell");
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseID];
cell.detailTextLabel.text=[NSString stringWithFormat:@"privious %d",indexPath.row];
}
NSString *str=dataSource[indexPath.section][indexPath.row];
cell.textLabel.text=str;
return cell;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
/*
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert
*/
if (editingStyle==UITableViewCellEditingStyleInsert) {
//1、insert a data
NSString *str=[NSString stringWithFormat:@“new the line %d",indexPath.row];
NSMutableArray *sectionArray=dataSource[indexPath.section];
[sectionArray insertObject:str atIndex:indexPath.row];
//2、insert a row in tableview
[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
}else if (editingStyle==UITableViewCellEditingStyleDelete){
//1、remove a row in datasource.
NSMutableArray *sectionArray=dataSource[indexPath.section];
[sectionArray removeObjectAtIndex:indexPath.row];
//2、remove a row in tableview.
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section%2==0) {
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleInsert;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSMutableArray *sourceSectionArray=dataSource[sourceIndexPath.section];
id sourceData=sourceSectionArray[sourceIndexPath.row];
[sourceSectionArray removeObject:sourceData];
//remove it in previous location
NSMutableArray *destinationSectionArray=dataSource[destinationIndexPath.section];
[destinationSectionArray insertObject:sourceData atIndex:destinationIndexPath.row];
//Insert the row in new location
[tableView reloadRowsAtIndexPaths:@[destinationIndexPath] withRowAnimation:UITableViewRowAnimationNone];
//refresh a row, but have some error in the application.
[tableView reloadData];
}

IOS UI 第九篇: UITABLEVIEW的更多相关文章
- iOS开发——UI_swift篇&UITableView实现单元格展开与隐藏
UITableView实现单元格展开与隐藏 关于UITableView的展开的收缩在前面的文章我已经结束,就是使用代理,通知,block传值的时候实现的,当时是使用一个Bool值来实现,最后使用着三 ...
- iOS开发——UI_swift篇&UITableView实现索引功能
UITableView实现索引功能 关于UItableView的索引在平时项目中所见不多,最多的就是跟联系人有关的界面,虽然如此,但是作为一个swift开发的程序必须知道的一个技术点,所以今天 ...
- IOS UI 第二篇:基本UI
1.UI书写: 一个小练习图片如下: 代码如下: @implementation AppDelegate{ UIControl *control; UILabel *l ...
- iOS开发——UI_swift篇&UItableView实现移动单元格
UItableView实现移动单元格 1,下面的样例是给表格UITableView添加单元格移动功能: (1)给表格添加长按功能,长按后表格进入编辑状态 (2)在编辑状态下,可以看到单元格后面出现 ...
- IOS UI 第一篇:基本UI
1. UI 书写 最基本创建一个label 标签 写一个first rate : UILabel *label = [[UILabel alloc] initWithFrame:CGRect ...
- IOS设计模式第九篇之备忘录模式
版权声明:原创作品,谢绝转载!否则将追究法律责任. 备忘录模式捕获和具体化对象的内部状态.换句话说,它可以节省你的东西后来,这种外部状态可以恢复在不违反封装; 也就是说,私人数据是私有的. 怎么用备忘 ...
- iOS开发UI篇—UITableview控件简单介绍
iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...
- iOS开发UI篇—UITableview控件基本使用
iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...
- iOS开发UI篇—UITableview控件使用小结
iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...
随机推荐
- DES加密和解密PHP,Java,ObjectC统一的方法
原文:DES加密和解密PHP,Java,ObjectC统一的方法 PHP的加解密函数 <?php class DesComponent { var $key = '12345678'; func ...
- 第一次测试HTML和CSS
1.HTML(Hyper Text Markup Languange)超文本标记语言.HTML文件扩展名通常是:htm和html. <html> <head> <titl ...
- Corel VideoStudio Pro X7(会声会影)
今天了解一天的视频剪辑方面的知识,自己也动手做了一个. 好啦!下面给大家一些建议: 剪辑软件选择: 1.易学易用.容易上手.模板丰富:会声会影:(需要安装包的可以留言和私信我)2.功能齐全.占用资源少 ...
- JSP具体条款——response对象
response对象 response为响应对象client要求.输出信息到客户.他封装JSP反应生成.发送client在回应client要求. 1.重定向网页 使用response对象的sendRe ...
- crawler_httpurlconnection_自动编码识别
核心思想: 1:从响应头中读取 [命中解流准确率最高] 2:如果响应头中没有,打开流从源码中读取,[取舍,如果有一般在前30行会有,前100行中寻找] 3:如果还没有,根据字节码code位置,字符识别 ...
- ReactJS.NET 开发
初探ReactJS.NET 开发 ReactJS通常也被称为"React",是一个刚刚在这场游戏中登场的新手.它由Facebook创建,并在2013年首次发布.Facebook ...
- 你知道OneNote的OCR功能吗?office lens为其增大威力,中文也识别
原文:[原创]你知道OneNote的OCR功能吗?office lens为其增大威力,中文也识别 OneNote提供了强大的从图片中取出文字的功能,大家只要装上了桌面版OneNote(本人用的2013 ...
- cocos2d-x多分辨率和随后的自适应CCListView的bug修复
cocos2d-x多分辨率自适配及因此导致的CCListView的bug修复 cocos2d-x是一款众所周知的跨平台的游戏开发引擎.因为其跨平台的特性.多分辨率支持也自然就有其需求. 因此.在某一次 ...
- csu 1503: 点弧之间的距离-湖南省第十届大学生计算机程序设计大赛
这是--比量p并用交点连接中心不上弧.在于:它至p距离.是不是p与端点之间的最短距离 #include<iostream> #include<map> #include< ...
- <C++ 实现设计模式> 观察者模式
观察者模式,又称公布--订阅,mvc模式等. 通俗点讲,比方股票来说,非常多人关注一支股票,派一个人去观察股票的情况,一有变化(观察),就通知全部的预定这个消息的人. 而我们常见的mvc模式,v是指v ...