主要讲解了UITableView的一些性质与应用
还有重写cell
//
// ViewController.h
// UiTableView
//
// Created by dashen on 15-4-27.
// Copyright (c) 2015年 李灵杰. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "WeChatCell.h"
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@end
//
// ViewController.m
// UiTableView
//
// Created by dashen on 15-4-27.
// Copyright (c) 2015年 李灵杰. All rights reserved.
//
#import "ViewController.h"
#import "WeChatCell.h"
@interface ViewController ()
@property (nonatomic,strong) UITableView *uiTableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.uiTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
_uiTableView.delegate = self;
_uiTableView.dataSource = self;
[self.view addSubview:_uiTableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 80;
}
//设置行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 4;
}
-(WeChatCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellID = @"cellID";
//先不忙着创建cell,表示图有一个队列,当cell移出界面(消失),消失的cell将会被扔到表示图的队列中去
//首先从这个队列里面去查看有没有可以重复利用的cell 这个cell的标识符是cellID,如果有直接返回这个cell
//如果不存在返回nil
WeChatCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
//判断是否存在
if(cell==nil){
//如果不存在 ,就自己创建一个cell,加上标签
cell = [[WeChatCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
NSLog(@"%ld",indexPath.row);
}
cell.headerImageView.image = [UIImage imageNamed:@"wechat"];
return cell;
}
/*
//设置段
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 27;
}
//设置每段的头header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [NSString stringWithFormat:@"%c",'A'+(int)section];
}
//设置每段段尾footer
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return @"bye";
}
//添加右侧索引表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSMutableArray *titlesArray = [NSMutableArray arrayWithCapacity:27];
for (int i= 0;i<26;i++){
NSString *alphaString = [NSString stringWithFormat:@"%c",'A'+i];
[titlesArray addObject:alphaString];
}
return titlesArray;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
return NO;
}else{
return YES;
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"删除";
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"commit");
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//取消选中状态
[self.uiTableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(@"selected");
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"deselect");
}
- (IBAction)EditBarButton:(UIBarButtonItem *)sender {
if([sender.title isEqualToString:@"Edit"]){
[sender setTitle:@"Done"];
[self.uiTableView setEditing:YES animated:YES];
}else {
[sender setTitle:@"Edit"];
[self.uiTableView setEditing:NO animated:YES];
}
}
*/
@end
//
// WeChatCell.h
// UiTableView
//
// Created by dashen on 15-4-28.
// Copyright (c) 2015年 李灵杰. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface WeChatCell : UITableViewCell
@property (nonatomic,strong) UIImageView *headerImageView;
@end
//
// WeChatCell.m
// UiTableView
//
// Created by dashen on 15-4-28.
// Copyright (c) 2015年 李灵杰. All rights reserved.
//
#import "WeChatCell.h"
@interface WeChatCell ()
@end
@implementation WeChatCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self= [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 60, 60)];
[self.contentView addSubview:_headerImageView];
}
return self;
}
@end