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

Tableciew的基本属性和侧滑(删除 置顶 更多)

时间:2016-03-18 21:49:34      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

 

#import <UIKit/UIKit.h>

 

//使用tableview必须遵循的

@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>

@property(strong,nonatomic) UITableView *tableview;

//数据源

@property(strong,nonatomic) NSArray *students;

@end

 

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

//    1.数据源

    self.students=@[@"dfsa",@"daf",@"dasf"];

    //    tableview 初始化 指定样式

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

//    3.指定代理

    self.tableview.delegate=self;

    

    self.tableview.dataSource=self;

    

//表格分隔符的颜色

    self.tableview.separatorColor=[UIColor redColor];

    

//    指定重用单元格的唯一标识

    [self.tableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

   //    4.添加到view上

    [self.view addSubview:self.tableview];

    

}

//设置显示分区数量

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}

 

 

#pragma mark 数据源 每个分区显示行数设置

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

{

    return self.students.count;

}

#pragma mark 每个单元格显示的内容

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

{

    static NSString *cellIdentity=@"cell";

    

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentity forIndexPath:indexPath];

    

    cell.textLabel.text=self.students[indexPath.row];

    

    return cell;

    

//    return [UITableViewCell new];

}

 

 

 

#pragma mark 代理方法 显示选中行的单元格信息

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

{

    NSLog(@"%@",self.students[indexPath.row]);

}

 

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

{

    return YES;

}

 

 

//侧滑 删除 置顶  更多 按钮

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

    

    UITableViewRowAction *layTopRowAction1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        NSLog(@"点击了删除");

        [tableView setEditing:NO animated:YES];

    }];

    layTopRowAction1.backgroundColor = [UIColor redColor];

    UITableViewRowAction *layTopRowAction2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        NSLog(@"点击了置顶");

        [tableView setEditing:NO animated:YES];

    }];

    layTopRowAction2.backgroundColor = [UIColor greenColor];

    UITableViewRowAction *layTopRowAction3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"更多" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        NSLog(@"点击了更多");

        [tableView setEditing:NO animated:YES];

        

    }];

    layTopRowAction3.backgroundColor = [UIColor blueColor];

    NSArray *arr = @[layTopRowAction1,layTopRowAction2,layTopRowAction3];

    return arr;

}

Tableciew的基本属性和侧滑(删除 置顶 更多)

标签:

原文地址:http://www.cnblogs.com/tianlianghong/p/5293627.html

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