关于iOS中的TableView

在使用tableview 前 记得设置 Delegate 和DataSource

UITableView = _TabView;

-(void)viewDidLoad{

[super viewDidLoad];

_TabView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

_TabView.delegate = self;

_TabView.dataSource = self;

//设置cell 的 分行样式 默认为UITableViewCellSeparatorStyleSingleLine

[_TabView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

//设置cell 行 分割线 的颜色

_TabView.separatorColor = [UIColor redColor];

//设置行高

_TabView.rowHeight = 120;

//设置行 Header 的高度

_TabView.sectionHeaderHeight = 100;

//设置行 Fotter 的高度

_TabView.sectionFooterHeight = 100;

//跳转到指定的 分区 和 行

[_TabView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:YES];

[self.view addSubview:_TabView];

}

//设置表格的分区数

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return 10;

}

//每个section底部标题高度(实现这个代理方法后前面 sectionHeaderHeight 设定的高度无效)

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

return 20;

}

//每个section头部标题高度(实现这个代理方法后前面 sectionFooterHeight 设定的高度无效)

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

return 20;

}

//设定每个分区section 头部的标题

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

return  @"header";

//return [[dictonary allKeys] objectAtIndex:section];字典 赋 给头部标题

}

//设定每隔分区的底部标题 footer

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

return  @"footer";

}

//用 自定制 自定义的 section头部视图-Header

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

return nil;

}

//用以定制自定义的section底部视图-Footer

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,320, 20)];

imageView.image=[UIImage imageNamed:@"1000.png"];

return imageView;

}

//设置每个分区内的行数

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

if (section == 0) {

return 10;

}else if(section == 1){

return 5;

}

return 0;

}

//创建cell 及 给cell 赋值{

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString * cellid = @"cellid";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];

if (!cell) {

cell  =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];

//设定选中cell时的cell的背影颜色

//选中时蓝色效果

cell.selectionStyle=UITableViewCellSelectionStyleBlue;

//cell.selectionStyle=UITableViewCellSelectionStyleNone; //选中时没有颜色效果

//cell.selectionStyle=UITableViewCellSelectionStyleGray;  //选中时灰色效果

//自定义选中cell时的背景颜色

//UIView *selectedView =[[UIViewalloc]initWithFrame:cell.contentView.frame];

//selectedView.backgroundColor = [UIColor orangeColor];

//cell.selectedBackgroundView = selectedView;

//[selectedView release];

//行不能被选中

// cell.selectionStyle=UITableViewCellAccessoryNone;

//这是设置没选中之前的背景颜色

cell.contentView.backgroundColor = [UIColor clearColor];

//未选cell时的图片

cell.imageView.image=[UIImage imageNamed:@"1001.jpg"];

//选中cell后的图片

cell.imageView.highlightedImage=[UIImage imageNamed:@"1002.jpg"];

}

//每一行上添加图片

//每一行上添加主标题

cell.textLabel.text = @"你好";

//每一行上添加子标题

cell.detailTextLabel.text = @"敦敦";

//从字典中获取数据

//cell.textLabel.text=[[self.myDic objectForKey:[[self.myDicallKeys]objectAtIndex:indexPath.section]]objectAtIndex:indexPath.row];

//从Model中获取数据

Model * model = arr[indexPath.row];(用到网络的时候要注明跳转时给下一个界面赋地址)

//每一行上添加按钮

return cell;

}

//改变行的高度(实现主个代理方法后 rowHeight 设定的高度无效)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 100;

}

//======================cell的点击跳转事件

//选中Cell响应事件

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{

//选中后的反显颜色即刻消失

[tableView deselectRowAtIndexPath:indexPath animated:YES];

//得到当前选中的cell

UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];

NSLog(@"cell=%@",cell);

//点击不同的cell 不同的响应事件

//    self.navigationController.navigationBar.hidden = NO;

//    if (indexPath.section == 0)

//    {

//        if (indexPath.row == 0)

//        {

//            NSLog(@"点击了1-1");

//        }

//        else if (indexPath.row == 1)

//        {

//            NSLog(@"点击了1-2");

//        }

//        else if (indexPath.row == 2)

//        {

//            NSLog(@"点击了1-3");

//        }

//    }else if (indexPath.section == 1)

//    {

//        if (indexPath.row == 1){

//            NSLog(@"点击了2-1");

//        }else if(indexPath.row == 2){

//            NSLog(@"点击了2-2");

//        }

//    }

}

------------------册通知时候

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificationUpdate:) name:@"data" object:nil];

调用业务处理类获取数据

[[LoadData shareLoadData]getData];

//设置通知响应方法

- (void)notificationUpdate:(NSNotification *)notifi

{

arr = [notifi.object copy];

//刷新表格

[table reloadData];

}

--------------------------------

//=====================cell 左滑时的事件

//cell的左滑删除事件

// 设置 cell 是否允许左滑

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

return true;

}

//// 设置默认的左滑按钮的title

//-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {

//    return @"按钮钮钮";

//}

//// 点击左滑出现的按钮时触发

//-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

//    NSLog(@"点击左滑出现的按钮时触发");

//}

//// 左滑结束时调用(只对默认的左滑按钮有效,自定义按钮时这个方法无效)

//-(void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {

//    NSLog(@"左滑结束");

//}

// 自定义左滑cell时的按钮和触发方法

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

// 创建第一个按钮和触发事件

UITableViewRowAction *cellActionA = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"按钮-1" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){

// 在此写点击按钮时的触发事件

// ......

NSLog(@"点击了 按钮-1");

}];

// 定义按钮的颜色

cellActionA.backgroundColor = [UIColor greenColor];

// 创建第二个按钮和触发事件

UITableViewRowAction *cellActionB = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"按钮-2" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){

// 在此写点击按钮时的触发事件

// ......

NSLog(@"点击了 按钮-2");

}];

// 注意这里返回的是一个按钮组,即使只定义了一个按钮也要返回一个组

return @[cellActionA, cellActionB];

}

//行缩进

//-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{

//    NSUInteger row = [indexPath row];

//    return row;

//}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容