UITableView 简单使用

//UITableViewStylePlain按行布局样式

UITableView*myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,375,667-64)style:UITableViewStylePlain];

myTableView.backgroundColor = [UIColor whiteColor];

//签两个代理人

myTableView.delegate =self;

myTableView.dataSource =self;

//去掉 cell 上的线

myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

//多余的cell不显示

self.myTableView.tableFooterView = [UIView new];

//取消cell点击

self.myTableView.allowsSelection =NO;

//******************

//设置 tableView 的表头视图可以放轮播图

UIView*headerView = [[UIViewalloc]initWithFrame:CGRectMake(0,0,375,200)];

headerView.backgroundColor = [UIColor grayColor];

//在这加上来就可以了设置 tableView 的表头视图

myTableView.tableHeaderView = headerView;

[headerView release];

[self.view addSubview:myTableView];

[myTableView release];

//刷新整个 TableView

[myTableView reloadData];

//刷新 某个 cell数组里是 要刷新的index 数组

//myTableView reloadRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>

//设置 背景图片

// UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];

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

//背景图片

//myTableView.backgroundView = imageView;

//[imageView release];

}

// cell 的行高

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

//选中 cell的 触发方法

- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    
    //NSLog(@" 我被选中了 %ld, %ld", indexPath.section, indexPath.row);
    
    //选中时有置灰效果,离开时 选中效果 消失
    
    [tableViewdeselectRowAtIndexPath:indexPathanimated:YES];
    
    //取消cell的点击事件
    
    if(2== indexPath.row) {
        
        //取消某行cell点击事件
        
        Cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        returnnightCell;
        
    }
    
    //自定义 一个 NSIndexPath用于滚到这行
    
    NSIndexPath*myIndex = [NSIndexPathindexPathForRow:2inSection:2];
    
    //滚动到某一行 cell
    
    [tableViewscrollToRowAtIndexPath:myIndexatScrollPosition:UITableViewScrollPositionBottomanimated:YES];
    
    //跳转 界面
    
    //CellViewController *cellView = [[CellViewController alloc]init];
    
    //NSString *str = [NSString stringWithFormat:@"%ld , %ld", indexPath.section, indexPath.row];
    
    //cellView.string =str;
    
    //[self.navigationController pushViewController:cellView animated:YES];
    
    //[cellView release];
    
}

//选中时不触发,当选中下一个时触发

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

//NSLog(@" 我是上一个选中时触发的 %ld, %ld", indexPath.section, indexPath.row);

//}

//设置 section 表头标题

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section {
    
    if(0== section) {
        
        return@"AA";
        
    }
    
    if(1== section) {
        
        return@"BB";
        
    }
    
    if(2== section) {
        
        return@"CC";
        
    }else{
        
        return@"DD";
        
    }
    
}

//索引

- (NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView {
    
    return[NSArrayarrayWithObjects:@"AA",@"BB",@"CC",@"DD",nil];
    
}

// 设置 Section 数 ,即:区数

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
    
    return4;
    
}

//***************************

//两个必须实现的方法

//设置每个 section 的行数

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    
    if(0== section) {
        
        return2;// 0 的区设置 2行
        
    }elseif(1== section) {
        
        return3;// 1 的区设置 3行
        
        //取消某行cell点击事件
        
        nightCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }else{
        
        return20;// 2 的行 设置 4行
        
    }
    
}

//设置 行的内容内部犹如一个大循环

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
    
    //1. 先从从用池取
    
    //queue:队列Reusable:重用Identifier:标示符
    
    //标示符 是区别不同 cell 唯一的标记
    
    staticNSString*cellIdentifier =@"cell";
    
    //给一个cell做标记
    
    UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentifier];
    
    //cell 为空的时候 ,需要创建 ,即:第一次创建,之后就不用创建了
    
    if(nil== cell) {
        
        //初始化一个UITableViewCellStyleDefault 是不显示 副标题的
        
        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:cellIdentifier];
        
        //cell 可以设置为透明
        
        cell.backgroundColor= [UIColorclearColor];
        
        //取消 cell的选中效果
        
        //cell.selectionStyle = UITableViewCellSelectionStyleNone;//这样用户体验不好 我们用下面这个:
        
        //选中时有置灰效果,离开时 选中效果 消失
        
        //这句话在选中cell时触发的方法里设置[tableView deselectRowAtIndexPath:indexPath animated:YES];
        
    }
    
    //0 区
    
    if(0== indexPath.section) {
        
        cell.textLabel.text=@"芦珊";// 主标签
        
        NSString*str = [NSStringstringWithFormat:@"(%ld , %ld)", indexPath.section, indexPath.row];
        
        cell.detailTextLabel.text= str;//副标题
        
        cell.imageView.image= [UIImageimageNamed:@"qq.png"];// 设置图片
        
    }
    
    //1 区
    
    if(1== indexPath.section) {
        
        cell.textLabel.text=@"涛哥";
        
        NSString*str = [NSStringstringWithFormat:@"(%ld , %ld)", indexPath.section, indexPath.row];
        
        cell.detailTextLabel.text= str;// 设置 副标题
        
    }
    
    //2 区
    
    if(2== indexPath.section) {
        
        cell.textLabel.text=@"大哥大哥";
        
        NSString*str = [NSStringstringWithFormat:@"(%ld , %ld)", indexPath.section, indexPath.row];
        
        cell.detailTextLabel.text= str;//设置 副标题
        
    }
    
    if(indexPath.section==1&& indexPath.row==2) {
        
        cell.textLabel.text=@"大水杯";
        
    }
    
    returncell;
    
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容