您的位置:首页 > 产品设计 > UI/UE

UI 15 UITableViewController & 系统自带快捷菜单 & 系统下拉刷新

2015-08-29 14:55 489 查看
UITableViewController 已经签订好tableView的两个协议,可以直接使用.

只要将必须完成的两个协议的内容写好即可.

下面的代码加入了系统默认的下拉刷新功能, 每次向下拉刷新时都添加一个@”哈哈”

代码实现如下:

#import "MainTableViewController.h"

@interface MainTableViewController ()
@property (nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)UIRefreshControl *RefreshControl;

@end

@implementation MainTableViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花容",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松",nil];
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];
// 系统默认的刷新, 下拉
self.RefreshControl = [[UIRefreshControl alloc] init];
self.RefreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"正在刷新"];
[self.view addSubview:self.RefreshControl];

[self.RefreshControl addTarget:self action:@selector(changeVaule:) forControlEvents:UIControlEventValueChanged];
}
- (void)changeVaule:(UIRefreshControl *)refresh{
// 先关闭刷新效果, 若不关闭就会一直显示.
[refresh endRefreshing];
NSString *str = @"哈哈";
[self.arr insertObject:str atIndex:0];
[self.tableView reloadData];
}


这是创建文件时就已经写好的协议方法

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuse] autorelease];
}
cell.textLabel.text = self.arr[indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:20];
// Configure the cell...

return cell;
}


当然, 协议中还有其他方法,比如, 有一个系统自带的快捷菜单

#pragma mark 设置是否允许给tableView上的cell添加快捷菜单方法.(协议中的)
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
#pragma mark 这个方法是设置是否允许给tableView的cell添加事件.
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
return YES;
}
#pragma mark 当点击菜单上的按钮之后,会出现的方法.
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
if (action == @selector(copy:)) {
NSLog(@"copy");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: