自定义UITableview 两种方法

第一:

#import "ViewController.h"

@interface ViewController ()

{

UITableView *tableview;

}

@property(nonatomic,strong) NSMutableDictionary *names;

@property(nonatomic,strong)NSArray *keys;

@end

@implementation ViewController

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return _keys.count;//分区

}

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

{

return _keys[section];

}

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

{

static NSString *[email protected]"cell";

UITableViewCell *cell= [tableview dequeueReusableCellWithIdentifier:cellID];

if (cell==nil) {

NSString *key=_keys[indexPath.section];

NSArray *nameSection=_names[key];

cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];

cell.textLabel.text=nameSection[indexPath.row];

}

return cell;

}

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

{

NSString *key=_keys[section];

NSArray *nameSection=_names[key];

return nameSection.count;

}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

return _keys;

}

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

//{

//    if (indexPath.section==1&& indexPath.row==0) {

//        return NO;//不可以编辑

//    }

//    //指定哪些行可以编辑

//    return YES;//可以编辑

//}

//以下在重写上面编辑的方法

-(void)setEditing:(BOOL)editing animated:(BOOL)animated

{

//重写方法,bar,关于右边添加的按钮 对tv有个自动的功能

[super setEditing:editing animated:animated];

NSLog(@"3333");

//红色圆圈出现

[tableview setEditing:editing animated:animated];

}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{//变成绿色的圆圈了

if (indexPath.section==0&& indexPath.row==0) {//第一组的第一行出现一个绿色圆圈

return UITableViewCellEditingStyleInsert;

}

return UITableViewCellEditingStyleDelete;

}

//commit提交,在单元格上左滑,会出现delete删除目标

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

{

//绿色加号

NSString *key=_keys[indexPath.section];//找到A B C组

NSMutableArray *arr=_names[key];//找到组里的东西

switch (editingStyle) {

case UITableViewCellEditingStyleDelete:

{

if (arr.count>1)

{

[arr removeObjectAtIndex:indexPath.row];//删除数组里的

[tableview deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];

//               [tableview reloadData];

}

else

{

[_names removeObjectForKey:key];

//重新给_keys赋值,因为等一下还要根据_keys算东西

_keys=[_names allKeys];

[tableview reloadData];

}

}

break;

case UITableViewCellEditingStyleInsert:

{

[arr insertObject:@"999" atIndex:1];

NSIndexPath *t=[NSIndexPath indexPathForRow: 1 inSection:indexPath.section];

[tableview insertRowsAtIndexPaths:@[t] withRowAnimation:UITableViewRowAnimationRight];

//动画效果

//           [tableview reloadData];

}

//           break;

default:

break;

}

//

//    //删除原始数据

//    NSString *key=_keys[indexPath.section];//找到A B C组

//

//    NSMutableArray *arr=_names[key];//找到组里的东西

//

//    if (arr.count>1)

//    {

//

//        [arr removeObjectAtIndex:indexPath.row];//删除数组里的

//        [tableview reloadData];

//

//    }

//    else

//    {

//        [_names removeObjectForKey:key];

//        //重新给_keys赋值,因为等一下还要根据_keys算东西

//        _keys=[_names allKeys];

//        [tableview reloadData];

//    }

//删除tableview上的 cell

}

-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

if (indexPath.section==1) {

return YES;

}

return YES;

}

-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath

{

NSMutableArray *array=_names[_keys[sourceIndexPath.section]];

NSString *name=array[sourceIndexPath.row];

//    NSString *newname=array[destinationIndexPath.row];

[array removeObjectAtIndex:sourceIndexPath.row];

[array insertObject:name atIndex:destinationIndexPath.row];//可以任意移动着玩

}

-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath

{

if (sourceIndexPath.section==proposedDestinationIndexPath.section) {

return proposedDestinationIndexPath;//不可以跨组移动

}

return sourceIndexPath;

}

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

{

NSLog(@"点的第几行%ld",(long)indexPath.row);

tableview.allowsMultipleSelection=YES;//允许多选;

NSArray *arr=tableview.indexPathsForSelectedRows;

NSLog(@"%@",arr[indexPath.section]);

//用这个方法,点击某行时,可以push 到另一个界面,点击这行有反应,所以可以调到另一个界面。

}

- (void)viewDidLoad {

// NSLog(@"--%@",NSHomeDirectory());

self.navigationItem.rightBarButtonItem=self.editButtonItem;//添加右边的按钮

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

//    tableview.frame=self.view.frame;

tableview.allowsMultipleSelection=YES;//允许多选

tableview.delegate=self;

tableview.dataSource=self;

[self.view addSubview:tableview];

NSMutableArray *arr1=[@[@"杭州",@"台湾",@"中国"]mutableCopy];

NSMutableArray *arr2=[@[@"上海",@"河南",@"中国"]mutableCopy];

NSMutableArray *arr3=[@[@"洛阳",@"杭州",@"中国"]mutableCopy];

_names=[@{@"A组":arr1,@"B组":arr2,@"C组":arr3}mutableCopy];

_keys=[[_names allKeys]sortedArrayUsingSelector:@selector(compare:)];

self.view.backgroundColor=[UIColor cyanColor];

[tableview setRowHeight:70];

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

第二:###################################################

MVC设计模式

C部分

#import "InterMusicView.h"

@implementation InterMusicView

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

{

MusicTableViewCell *cell=[_tableview dequeueReusableCellWithIdentifier:@"MusicTableViewCell"];

InterMusicModel *model=self.arr[indexPath.row];

cell.intermusic=model;

cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;//添加了一个小箭头;

return cell;

}

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

{

return _arr.count;

}

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

{

return 100;

}

-(id)initWithFrame:(CGRect)frame

{

if (self=[super  initWithFrame:frame]) {

self.backgroundColor=[UIColor lightGrayColor];

_tableview=[[UITableView alloc]init];

_tableview.frame=CGRectMake(10, 60, 355, 500);

_tableview.delegate=self;

_tableview.dataSource=self;

self.arr=[[InterMusicModel musicModel]mutableCopy];

[_tableview registerNib:[UINib nibWithNibName:@"MusicTableViewCell" bundle:nil] forCellReuseIdentifier:@"MusicTableViewCell"];

UILabel *lable=[UILabel new];

lable.frame=CGRectMake(10, 5, 355, 50);

lable.backgroundColor=[UIColor redColor];

NSArray *[email protected][@"内置音乐",@"示例音乐"];

UISegmentedControl *seg=[[UISegmentedControl alloc]initWithItems:arr];

seg.tintColor=[UIColor whiteColor];

[seg addTarget:self action:@selector(exchang:) forControlEvents:UIControlEventEditingChanged];

seg.frame=CGRectMake(100, 9, 200, 40);

[self addSubview:lable];

[self addSubview:seg];

[self addSubview:_tableview];

}

return self;

}

-(void)exchang:(UISegmentedControl *)segm

{

}

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

//{

//    UILabel *lable=[UILabel new];

//    lable.frame=CGRectMake(0, 0, 375, 100);

//    if (section==0) {

//        NSArray *[email protected][@"内置音乐",@"示例音乐"];

//        UISegmentedControl *seg=[[UISegmentedControl alloc]initWithItems:arr];

//        seg.tintColor=[UIColor whiteColor];

//        seg.frame=CGRectMake(100, 5, 200, 40);

//        [_tableview addSubview:seg];

//    }

//

//

//    lable.backgroundColor=[UIColor redColor];

// //   [_tableview addSubview:lable];

//    return lable;

//}

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

//{

//    return 50;

//}

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

//{

//    return 1;

//}

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

//{

//    return @"道歉";

//}

时间: 2024-07-31 22:39:55

自定义UITableview 两种方法的相关文章

Apache服务器自定义404页面的两种方法以及.htaccess的重要命令总结

Apache服务器自定义404错误页面有两种方法: 第一种方法最简单,直接在Apache的httpd.conf下进行配置修改命令,修改的内容请参看.htaccess命令写法中的自定义错误页面 第二种方法可以在.htaccess文件中配置命令,具体操作请参看.htaccess命令写法中的自定义错误页面 .htaccess使用方法总结 1 . 首先让的本地APACHE服务器器允许.htaccess修改 打开httpd.conf (1) Options FollowSymLinks AllowOver

Linux中生成密钥的两种方法

Linux中生成密钥的两种方法 SSH服务支持一种安全认证机制,即密钥认证.所谓的密钥认证,实际上是使用一对加密字符串,一个称为公钥(publickey), 任何人都可以看到其内容,用于加密:另一个称为密钥(privatekey),只有拥有者才能看到,用于解密.通过公钥加密过的密文使用密钥可以轻松解密,但根据公钥来猜测密钥却十分困难. ssh的密钥认证就是使用了这一特性.服务器和客户端都各自拥有自己的公钥和密钥.如何使用密钥认证登录linux服务器呢? 在使用密钥认证远程登入linux之前,我们

zabbix使用自己编写脚本模板和zabbix自带模板两种方法添加对指定进程和端口的监控

zabbix使用自己编写脚本模板和zabbix自带模板两种方法添加对指定进程和端口的监控 1.自带监控模板进行os的监控 进入/usr/local/zabbix/etc/zabbix_agentd.conf 配置文件修改 LogRemoteCommands=1     ###开启脚本功能 Server=192.168.5.129     ##修改zabbix指向的服务器: 重启zabbix_agentd.zabbix_server服务 在配置-->主机-->添加主机--> 配置主机信息主

(转)java创建线程的两种方法比较

Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线程,有两种方法: ◆需要从Java.lang.Thread类派生一个新的线程类,重载它的run()方法:  ◆实现Runnalbe接口,重载Runnalbe接口中的run()方法. 为什么Java要提供两种方法来创建线程呢?它们都有哪些区别?相比而言,哪一种方法更好呢? 在Java中,类仅支持单继承

C#实现Dll(OCX)控件自动注册的两种方法 网上找的 然后 自己试了试 还是可以用的

尽管MS为我们提供了丰富的.net framework库,我们的程序C#开发带来了极大的便利,但是有时候,一些特定功能的控件库还是需要由第三方提供或是自己编写.当需要用到Dll引用的时候,我们通常会通过“添加引用”的方式将它们纳入到项目中,然后就可以像使用自己的类一样方便的使用它们了.但是,有些Dll库(OCX)文件是需要注册到Windows注册表后才能正常添加和使用的.本文介绍两种为Dll库(OCX)自动注册的方法,为大家提供参考. 首先,大家都知道在Windows的“运行”中,输入“Regs

C# web api 返回类型设置为json的两种方法

每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不爱说话,默默承受着编程的巨大压力,除了技术上的交流外,他们不愿意也不擅长和别人交流,更不乐意任何人走进他们的内心! 悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来.我们都知道计算机技术发展日新月异,速度惊人的快,你我稍不留神,就会被慢慢淘汰!因此:每日不间断的

ASP.NET MVC 实现AJAX跨域请求的两种方法

通常发送AJAX请求都是在本域内完成的,也就是向本域内的某个URL发送请求,完成部分页面的刷新.但有的时候需要向其它域发送AJAX请求,完成数据的加载,例如Google. 在ASP.NET MVC 框架里实现跨域的AJAX请求有几种方式可以实现,以下就介绍常用的两种方法. 1.     发送JSONP请求 客户端: JQuery对发送JSONP请求有很好的支持,客户端通过. ajax() 函数发送请求,其中需要制定 dataType 为“jsonp”  jsonpCallback 为指定的回调函

Java 创建线程的两种方法

Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线程,有两种方法: ◆需要从Java.lang.Thread类派生一个新的线程类,重载它的run()方法: ◆实现Runnalbe接口,重载Runnalbe接口中的run()方法. 为什么Java要提供两种方法来创建线程呢?它们都有哪些区别?相比而言,哪一种方法更好呢? 在Java中,类仅支持单继承,

在Java Web程序中使用监听器可以通过以下两种方法

之前学习了很多涉及servlet的内容,本小结我们说一下监听器,说起监听器,编过桌面程序和手机App的都不陌生,常见的套路都是拖一个控件,然后给它绑定一个监听器,即可以对该对象的事件进行监听以便发生响应,从本质上来说这些都是观察者模式的具体实现,在web程序中的监听器也不例外.在Java Web程序中使用监听器可以通过以下两种方法:通过注解@WebListener来标识一个自定义的监听器:[java] view plain copy@WebListener public class Custom