码迷,mamicode.com
首页 > 其他好文 > 详细

自定义UITableview 两种方法

时间:2015-04-09 13:30:05      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

第一:

#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 *cellID=@"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 *arr=@[@"内置音乐",@"示例音乐"];

        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 *arr=@[@"内置音乐",@"示例音乐"];

//        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 @"道歉";

//}

 

自定义UITableview 两种方法

标签:

原文地址:http://www.cnblogs.com/linximu/p/4409352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!