用UITableView做一个有刷新和清除的简单视图
—这是我的第一个博客–
代码如下:
#import "AppDelegate.h"
#import "MainViewController.h"
#import "PhotoAlbumViewController.h"
#import "MusicViewController.h"
#import "SetViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
//标签控制器
MainViewController *mainVC = [[MainViewController alloc] init];
UINavigationController *mainNC = [[UINavigationController alloc] initWithRootViewController:mainVC];
PhotoAlbumViewController *photoAlbumVC = [[PhotoAlbumViewController alloc] init];
UINavigationController *photoAlbumNC = [[UINavigationController alloc] initWithRootViewController:photoAlbumVC];
MusicViewController *musicVC = [[MusicViewController alloc] init];
UINavigationController *musicNC = [[UINavigationController alloc] initWithRootViewController:musicVC];
SetViewController *setVC = [[SetViewController alloc] init];
UINavigationController *setNC = [[UINavigationController alloc] initWithRootViewController:setVC];
UITabBarController *tabBarNC = [[UITabBarController alloc] init];
tabBarNC.viewControllers = @[mainNC,photoAlbumNC,musicNC,setNC];
tabBarNC.tabBar.barTintColor = [UIColor whiteColor];
tabBarNC.tabBar.tintColor = [UIColor blackColor];
self.window.rootViewController = tabBarNC;
return YES;
}
#import "MainViewController.h"
@interface MainViewController () <UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSString *string;
@property (nonatomic, strong) NSMutableArray *stringArray;
@end
@implementation MainViewController
- (instancetype)init
{
self = [super init];
if (self) {
self.title = @"主页";
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:self.title image:[UIImage imageNamed:@"tab1"] tag:200];
self.tabBarItem = item;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
//刷新按钮
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithTitle:@"刷新" style:UIBarButtonItemStylePlain target:self action:@selector(refreshItemTaped:)];
self.navigationItem.leftBarButtonItem = refreshItem;
//清除按钮
UIBarButtonItem *clearItem = [[UIBarButtonItem alloc] initWithTitle:@"清除" style:UIBarButtonItemStylePlain target:self action:@selector(clearItemTaped:)];
self.navigationItem.rightBarButtonItem = clearItem;
//表格
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//数据源协议
_tableView.dataSource = self;
[self.view addSubview:_tableView];
//初始化
_stringArray = [NSMutableArray array];
}
//指定分区有多少行 required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _stringArray.count;
}
//配置每一行怎样显示 required
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
cell.detailTextLabel.text = _stringArray[indexPath.row];
NSString *indexString = [NSString stringWithFormat:@"index%ld",indexPath.row];
cell.textLabel.text = indexString;
if (indexPath.row % 2 == 0) {
cell.backgroundColor = [UIColor lightGrayColor];
}
return cell;
}
- (void)refreshItemTaped:(UIBarButtonItem *)sender{
//模拟网络请求
[self performSelector:@selector(loadDataSource) withObject:self afterDelay:1];
}
- (void)clearItemTaped:(UIBarButtonItem *)sender{
_stringArray = [[NSMutableArray alloc] init];
[_tableView reloadData];
}
- (void)loadDataSource{
//获取当前时间
NSDate *date = [[NSDate alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
_string = [formatter stringFromDate:date];
[_stringArray addObject:_string];
[_tableView reloadData];
}
@end
快捷键
- 加粗
Ctrl + B
- 斜体
Ctrl + I
- 引用
Ctrl + Q
- 插入链接
Ctrl + L
- 插入代码
Ctrl + K
- 插入图片
Ctrl + G
- 提升标题
Ctrl + H
- 有序列表
Ctrl + O
- 无序列表
Ctrl + U
- 横线
Ctrl + R
- 撤销
Ctrl + Z
- 重做
Ctrl + Y