iOS UI-表格控制器(UITableView)-基本使用
tableView的常见属性
cell的常见属性
一、一般情况
#import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @property (strong, nonatomic) UITableView *tableview; @property (strong, nonatomic) NSArray *dataSource; @end /*
UITableView
表格视图:两个协议,两个代理,四个方法 两个协议:
1.数据源协议:(提供表格数据,提供表格内容)UITableViewDataSource
2.执行代理协议:(提供表格操作方法)UITableViewDelegate
*/ @implementation ViewController #pragma mark - LifeCircle
- (void)viewDidLoad {
[super viewDidLoad]; [self.view setBackgroundColor:[UIColor groupTableViewBackgroundColor]]; self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(, , , ) style:UITableViewStyleGrouped]; self.tableview.delegate = self;
self.tableview.dataSource = self; [self.view addSubview:self.tableview]; NSArray *arr1 = @[@"芃,你好1",@"芃,你好2",@"芃,你好3",@"芃,你好4",@"芃,你好5"];
NSArray *arr2 = @[@"芃,你好6",@"芃,你好7",@"芃,你好8",@"芃,你好9"];
self.dataSource = @[arr1,arr2]; } #pragma mark - UITableViewDataSource
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.dataSource count];
} //设置每组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.dataSource objectAtIndex:section] count];
} // 设置[每一个]单元格方法
// 1.设置复用[单元格]ID
// 2.设置复用
// 3.创建单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.设置复用[单元格]ID
static NSString *cellIdentifier = @"cellIdentifier";
// 2.设置复用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// 3.创建单元格
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [[self.dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"1.jpg"];
cell.detailTextLabel.text = @"张芃芃的信息";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell;
} #pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//拿到点击的那一行
UITableViewCell *selectCell = [tableView cellForRowAtIndexPath:indexPath]; NSString *titleInRow = [NSString stringWithFormat:@"%@",selectCell.textLabel.text]; // NSString *titleInRow = [NSString stringWithFormat:@"%@",[[self.dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"问候" message:titleInRow delegate:self cancelButtonTitle:@"你好" otherButtonTitles: nil];
[alert show];
} @end
二、通过加载plist文件显示分组数据
#import <Foundation/Foundation.h> @interface Group : NSObject
//组标题
@property (copy, nonatomic) NSString *title;
//组描述
@property (copy, nonatomic) NSString *desc;
//汽车品牌信息
@property (strong, nonatomic) NSArray *cars; - (instancetype) initWithDict:(NSDictionary *)dict;
+ (instancetype) groupWithDict:(NSDictionary *)dict; @end #import "Group.h" @implementation Group - (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
// self.title = dict[@"title"];
// self.desc = dict[@"desc"];
// self.cars = dict[@"cars"];
[self setValuesForKeysWithDictionary:dict];
}
return self;
} + (instancetype)groupWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
} @end #import "ViewController.h"
#import "Group.h" @interface ViewController ()<UITableViewDataSource> @property (strong, nonatomic) NSArray *groups;
@property (strong, nonatomic) UITableView *tableView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.tableView.dataSource = self;
[self.view addSubview:self.tableView]; } #pragma mark - 数据源协议 //设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.groups count];
}
//设置每组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
Group *group = self.groups[section];
return group.cars.count; }
//设置每组每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.设置复用单元格ID
static NSString *cellIdentifier = @"cellIdentifier";
//2.设置复用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//3.创建单元格
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
// 3.1获取模型数据
//获取模型
Group *group = self.groups[indexPath.section];
//获取对应的汽车品牌
NSString *brand = group.cars[indexPath.row];
// 3.2把模型中的数据设置给单元格中的子控件
cell.textLabel.text = brand; return cell;
} //组标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
Group *group = self.groups[section];
return group.title;
}
//设置组尾
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
Group *group = self.groups[section];
return group.desc;
} #pragma mark - 懒加载
- (NSArray *)groups
{
if (_groups == nil) {
// 1.找到plist文件的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_simple.plist" ofType:nil];
// 2.加载plist文件
NSArray *array = [NSArray arrayWithContentsOfFile:path];
// 3.字典转模型
NSMutableArray *arrayModel = [NSMutableArray array];
// 4.遍历字典数组中的每个字典,把每个字典转成模型,把模型放到arrayModel
for (NSDictionary *tempDict in array) {
//创建模型对象
Group *model = [[Group alloc] initWithDict:tempDict];
[arrayModel addObject:model];
}
_groups =arrayModel; }
return _groups;
} #pragma mark - 设置状态栏文字的颜色
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
} #pragma mark - 隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
return YES;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
三、通过加载plist文件展示单组数据
#import <Foundation/Foundation.h> @interface HMLOL : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *intro; - (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)lolWithDict:(NSDictionary *)dict;
@end #import "HMLOL.h" @implementation HMLOL
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
} + (instancetype)lolWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict];
}
@end #import "ViewController.h"
#import "HMLOL.h"
@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
// 存放所有模型数据
@property (nonatomic, strong) NSArray *heroes;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 1.设置数据源
self.tableView.dataSource = self;
} //
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// return 1;
//} // 返回多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.heroes.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// 1.创建cell
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; // 2.取出模型中的数据设置给cell
HMLOL *hero = self.heroes[indexPath.row];
// cell内部显示内容的三个子控件
cell.textLabel.text = hero.name;
cell.imageView.image = [UIImage imageNamed:hero.icon];
cell.detailTextLabel.text = hero.intro; // 设置辅助视图的样式
// cell.accessoryType = UITableViewCellAccessoryNone;
// 设置自定义的辅助视图
// cell.accessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
// 3.返回给tabelView
return cell;
} #pragma mark - 懒加载
- (NSArray *)heroes {
if (_heroes == nil) {
NSArray *dictArr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]];
NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:dictArr.count];
for (NSDictionary *dict in dictArr) {
HMLOL *hero = [HMLOL lolWithDict:dict];
[arrM addObject:hero];
}
_heroes = arrM;
}
return _heroes;
}
@end
四、汽车品牌展示
#import <UIKit/UIKit.h> @interface BWCar : UIView
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon; -(instancetype) initWithDict:(NSDictionary *)dict;
+(instancetype) carWithDict:(NSDictionary *)dict; @end #import "BWCar.h" @implementation BWCar - (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
} + (instancetype)carWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
} @end #import <UIKit/UIKit.h> @interface BWGroup : UIView @property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) NSArray *cars; - (instancetype) initWithDict:(NSDictionary *)dict;
+ (instancetype) groupWithDict:(NSDictionary *)dict; @end #import "BWGroup.h"
#import "BWCar.h" @implementation BWGroup - (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
//当有模型嵌套的时候需要手动把字典转模型
//模型数组
NSMutableArray *arrayModels = [NSMutableArray array];
//字典转模型
for (NSDictionary *tempDict in self.cars) {
BWCar *model = [[BWCar alloc] initWithDict:tempDict];
[arrayModels addObject:model];
}
self.cars = arrayModels;
} return self;
} + (instancetype)groupWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
} @end #import "ViewController.h"
#import "BWCar.h"
#import "BWGroup.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate> @property (nonatomic, strong) NSArray *groups;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSIndexPath *indexPath; @end @implementation ViewController #pragma mark - 懒加载数据
- (NSArray *)groups
{
if (_groups == nil) { NSString *path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil];
NSArray *arr = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *arrayModels = [NSMutableArray array];
for (NSDictionary *dict in arr) {
BWGroup *group = [BWGroup groupWithDict:dict];
[arrayModels addObject:group];
}
_groups = arrayModels;
}
return _groups;
} - (void)viewDidLoad {
[super viewDidLoad]; self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
self.tableView.dataSource =self;
self.tableView.delegate =self; [self.view addSubview:self.tableView]; }
#pragma mark - 数据源方法
//加载数组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.groups.count;
}
//加载每组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
BWGroup *group = [self.groups objectAtIndex:section];
return group.cars.count;
} //加载单元格数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.创建单元格 // 1.1 声明重用ID
static NSString *cellIdentifier = @"cellIdentifier";
// 1.2 根据重用ID去缓存池中取对应的cell对象
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// 1.3 如果没有取到,就创建一个cell
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
// 2.设置单元格内容 // 2.1 取数据模型
BWGroup *group = self.groups[indexPath.section];
BWCar *car = group.cars[indexPath.row]; // 2.2 加载单元格内容
cell.textLabel.text = car.name;
cell.imageView.image = [UIImage imageNamed:car.icon]; // 3.返回单元格
return cell;
} //加载组标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
BWGroup *group = [self.groups objectAtIndex:section];
return group.title;
} //设置右侧索引兰
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [self.groups valueForKey:@"title"];
} #pragma mark - 代理方法
//监听行被选中的代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//引用indexPath
self.indexPath = indexPath; //获取当前被选中行的车的对象
BWGroup *group = self.groups[indexPath.section];
BWCar *car = group.cars[indexPath.row]; //创建一个对话框对象
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"编辑" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil]; //修改UIAlertView的样式
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
//获取那个文本框,并且设置文本框中的文字为car.name
[alert textFieldAtIndex:].text = car.name;
//显示对话框
[alert show];
}
//UIAlertView的按钮被点击了,就会执行
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//表示点击的是"确定"
if (buttonIndex == ) {
// 更新数据
// 1.获取用户文本框中的内容
NSString *name = [alertView textFieldAtIndex:].text; // 2.找到对应的车模型
// BWCar *car = self.groups[alertView.tag];
BWGroup *group = self.groups[self.indexPath.section];
BWCar *car = group.cars[self.indexPath.row];
// NSLog(@"%@",self.indexPath); // 3.修改车模型的name
car.name = name; // 4.刷新tableView(重新调用UITableView的数据源对象中的数据源方法) // reloadData刷新整个tableView
// [self.tableView reloadData]; //局部刷新
//创建一个行对象
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.indexPath.row inSection:self.indexPath.section];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
} @end
五、UITableView的API
//
// UITableView.h
// UIKit
//
// Copyright (c) 2005-2014 Apple Inc. All rights reserved.
// #import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <UIKit/UIScrollView.h>
#import <UIKit/UISwipeGestureRecognizer.h>
#import <UIKit/UITableViewCell.h>
#import <UIKit/UIKitDefines.h> typedef NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain, // regular table view
UITableViewStyleGrouped // preferences style table view
}; typedef NS_ENUM(NSInteger, UITableViewScrollPosition) {
UITableViewScrollPositionNone,
UITableViewScrollPositionTop,
UITableViewScrollPositionMiddle,
UITableViewScrollPositionBottom
}; // scroll so row of interest is completely visible at top/center/bottom of view typedef NS_ENUM(NSInteger, UITableViewRowAnimation) {
UITableViewRowAnimationFade,
UITableViewRowAnimationRight, // slide in from right (or out to right)
UITableViewRowAnimationLeft,
UITableViewRowAnimationTop,
UITableViewRowAnimationBottom,
UITableViewRowAnimationNone, // available in iOS 3.0
UITableViewRowAnimationMiddle, // available in iOS 3.2. attempts to keep cell centered in the space it will/did occupy
UITableViewRowAnimationAutomatic = // available in iOS 5.0. chooses an appropriate animation style for you
}; // Including this constant string in the array of strings returned by sectionIndexTitlesForTableView: will cause a magnifying glass icon to be displayed at that location in the index.
// This should generally only be used as the first title in the index.
UIKIT_EXTERN NSString *const UITableViewIndexSearch NS_AVAILABLE_IOS(3_0); // Returning this value from tableView:heightForHeaderInSection: or tableView:heightForFooterInSection: results in a height that fits the value returned from
// tableView:titleForHeaderInSection: or tableView:titleForFooterInSection: if the title is not nil.
UIKIT_EXTERN const CGFloat UITableViewAutomaticDimension NS_AVAILABLE_IOS(5_0); @class UITableView;
@class UINib;
@protocol UITableViewDataSource;
@class UILongPressGestureRecognizer;
@class UITableViewHeaderFooterView;
@class UIRefreshControl;
@class UIVisualEffect; typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) {
UITableViewRowActionStyleDefault = ,
UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault,
UITableViewRowActionStyleNormal
} NS_ENUM_AVAILABLE_IOS(8_0); NS_CLASS_AVAILABLE_IOS(8_0) @interface UITableViewRowAction : NSObject <NSCopying> + (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler; @property (nonatomic, readonly) UITableViewRowActionStyle style;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) UIColor *backgroundColor; // default background color is dependent on style
@property (nonatomic, copy) UIVisualEffect* backgroundEffect; @end //_______________________________________________________________________________________________________________
// this represents the display and behaviour of the cells. @protocol UITableViewDelegate<NSObject, UIScrollViewDelegate> @optional // Display customization - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didEndDisplayingFooterView:(UIView *)view forSection:(NSInteger)section NS_AVAILABLE_IOS(6_0); // Variable height support - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; // Use the estimatedHeight methods to quickly calcuate guessed values which will allow for fast load times of the table.
// If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be displayed, so more expensive logic can be placed there.
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0);
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section NS_AVAILABLE_IOS(7_0); // Section header & footer information. Views are preferred over title should you decide to provide both - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; // custom view for header. will be adjusted to default or specified header height
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; // custom view for footer. will be adjusted to default or specified footer height // Accessories (disclosures). - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath NS_DEPRECATED_IOS(2_0, 3_0);
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath; // Selection // -tableView:shouldHighlightRowAtIndexPath: is called when a touch comes down on a row.
// Returning NO to that message halts the selection process and does not cause the currently selected row to lose its selected look while the touch is down.
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
- (void)tableView:(UITableView *)tableView didUnhighlightRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0); // Editing // Allows customization of the editingStyle for a particular cell located at 'indexPath'. If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0); // supercedes -tableView:titleForDeleteConfirmationButtonForRowAtIndexPath: if return value is non-nil // Controls whether the background is indented while editing. If not implemented, the default is YES. This is unrelated to the indentation level below. This method only applies to grouped style table views.
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath; // The willBegin/didEnd methods are called whenever the 'editing' property is automatically changed by the table (allowing insert/delete/move). This is done by a swipe activating a single row
- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath;
- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath; // Moving/reordering // Allows customization of the target row for a particular row as it is being moved/reordered
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath; // Indentation - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; // return 'depth' of row for hierarchies // Copy/Paste. All three methods must be implemented by the delegate. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0);
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0);
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0); @end UIKIT_EXTERN NSString *const UITableViewSelectionDidChangeNotification; //_______________________________________________________________________________________________________________ NS_CLASS_AVAILABLE_IOS(2_0) @interface UITableView : UIScrollView <NSCoding> - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style; // must specify style at creation. -initWithFrame: calls this with UITableViewStylePlain @property (nonatomic, readonly) UITableViewStyle style;
@property (nonatomic, assign) id <UITableViewDataSource> dataSource;
@property (nonatomic, assign) id <UITableViewDelegate> delegate;
@property (nonatomic) CGFloat rowHeight; // will return the default value if unset
@property (nonatomic) CGFloat sectionHeaderHeight; // will return the default value if unset
@property (nonatomic) CGFloat sectionFooterHeight; // will return the default value if unset
@property (nonatomic) CGFloat estimatedRowHeight NS_AVAILABLE_IOS(7_0); // default is 0, which means there is no estimate
@property (nonatomic) CGFloat estimatedSectionHeaderHeight NS_AVAILABLE_IOS(7_0); // default is 0, which means there is no estimate
@property (nonatomic) CGFloat estimatedSectionFooterHeight NS_AVAILABLE_IOS(7_0); // default is 0, which means there is no estimate
@property (nonatomic) UIEdgeInsets separatorInset NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // allows customization of the frame of cell separators @property(nonatomic, readwrite, retain) UIView *backgroundView NS_AVAILABLE_IOS(3_2); // the background view will be automatically resized to track the size of the table view. this will be placed as a subview of the table view behind all cells and headers/footers. default may be non-nil for some devices. // Data - (void)reloadData; // reloads everything from scratch. redisplays visible rows. because we only keep info about visible rows, this is cheap. will adjust offset if table shrinks
- (void)reloadSectionIndexTitles NS_AVAILABLE_IOS(3_0); // reloads the index bar. // Info - (NSInteger)numberOfSections;
- (NSInteger)numberOfRowsInSection:(NSInteger)section; - (CGRect)rectForSection:(NSInteger)section; // includes header, footer and all rows
- (CGRect)rectForHeaderInSection:(NSInteger)section;
- (CGRect)rectForFooterInSection:(NSInteger)section;
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath; - (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point; // returns nil if point is outside of any row in the table
- (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell; // returns nil if cell is not visible
- (NSArray *)indexPathsForRowsInRect:(CGRect)rect; // returns nil if rect not valid - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; // returns nil if cell is not visible or index path is out of range
- (NSArray *)visibleCells;
- (NSArray *)indexPathsForVisibleRows;
- (UITableViewHeaderFooterView *)headerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);
- (UITableViewHeaderFooterView *)footerViewForSection:(NSInteger)section NS_AVAILABLE_IOS(6_0); - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated; // Row insertion/deletion/reloading. - (void)beginUpdates; // allow multiple insert/delete of rows and sections to be animated simultaneously. Nestable
- (void)endUpdates; // only call insert/delete/reload calls or change the editing state inside an update block. otherwise things like row count, etc. may be invalid. - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection NS_AVAILABLE_IOS(5_0); - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation NS_AVAILABLE_IOS(3_0);
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath NS_AVAILABLE_IOS(5_0); // Editing. When set, rows show insert/delete/reorder controls based on data source queries @property (nonatomic, getter=isEditing) BOOL editing; // default is NO. setting is not animated.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated; @property (nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0); // default is YES. Controls whether rows can be selected when not in editing mode
@property (nonatomic) BOOL allowsSelectionDuringEditing; // default is NO. Controls whether rows can be selected when in editing mode
@property (nonatomic) BOOL allowsMultipleSelection NS_AVAILABLE_IOS(5_0); // default is NO. Controls whether multiple rows can be selected simultaneously
@property (nonatomic) BOOL allowsMultipleSelectionDuringEditing NS_AVAILABLE_IOS(5_0); // default is NO. Controls whether multiple rows can be selected simultaneously in editing mode // Selection - (NSIndexPath *)indexPathForSelectedRow; // returns nil or index path representing section and row of selection.
- (NSArray *)indexPathsForSelectedRows NS_AVAILABLE_IOS(5_0); // returns nil or a set of index paths representing the sections and rows of the selection. // Selects and deselects rows. These methods will not call the delegate methods (-tableView:willSelectRowAtIndexPath: or tableView:didSelectRowAtIndexPath:), nor will it send out a notification.
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
- (void)deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated; // Appearance @property (nonatomic) NSInteger sectionIndexMinimumDisplayRowCount; // show special section index list on right when row count reaches this value. default is 0
@property (nonatomic, retain) UIColor *sectionIndexColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; // color used for text of the section index
@property (nonatomic, retain) UIColor *sectionIndexBackgroundColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // the background color of the section index while not being touched
@property (nonatomic, retain) UIColor *sectionIndexTrackingBackgroundColor NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR; // the background color of the section index while it is being touched @property (nonatomic) UITableViewCellSeparatorStyle separatorStyle; // default is UITableViewCellSeparatorStyleSingleLine
@property (nonatomic, retain) UIColor *separatorColor UI_APPEARANCE_SELECTOR; // default is the standard separator gray
@property (nonatomic, copy) UIVisualEffect *separatorEffect NS_AVAILABLE_IOS(8_0) UI_APPEARANCE_SELECTOR; // effect to apply to table separators @property (nonatomic, retain) UIView *tableHeaderView; // accessory view for above row content. default is nil. not to be confused with section header
@property (nonatomic, retain) UIView *tableFooterView; // accessory view below content. default is nil. not to be confused with section footer - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier; // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered
- (id)dequeueReusableHeaderFooterViewWithIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0); // like dequeueReusableCellWithIdentifier:, but for headers/footers // Beginning in iOS 6, clients can register a nib or class for each cell.
// If all reuse identifiers are registered, use the newer -dequeueReusableCellWithIdentifier:forIndexPath: to guarantee that a cell instance is returned.
// Instances returned from the new dequeue method will also be properly sized when they are returned.
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0);
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0); - (void)registerNib:(UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
- (void)registerClass:(Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0); @end //_______________________________________________________________________________________________________________
// this protocol represents the data model object. as such, it supplies no information about appearance (including the cells) @protocol UITableViewDataSource<NSObject> @required - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; @optional - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // fixed font style. use custom view (UILabel) if you want something different
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section; // Editing // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; // Moving/reordering // Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath; // Index - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; // return list of section titles to display in section index view (e.g. "ABCD...Z#")
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; // tell table which section corresponds to section title/index (e.g. "B",1)) // Data manipulation - insert and delete support // After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
// Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; // Data manipulation - reorder / moving support - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath; @end //_______________________________________________________________________________________________________________ // This category provides convenience methods to make it easier to use an NSIndexPath to represent a section and row
@interface NSIndexPath (UITableView) + (NSIndexPath *)indexPathForRow:(NSInteger)row inSection:(NSInteger)section; @property(nonatomic,readonly) NSInteger section;
@property(nonatomic,readonly) NSInteger row; @end
iOS UI-表格控制器(UITableView)-基本使用的更多相关文章
- IOS UI 第九篇: UITABLEVIEW
学英语.所以用英文来记录笔记. Define the dataSource: @implementation ViewController{ NSMutableArray *dataSo ...
- iOS开发UI篇—实现UItableview控件数据刷新
iOS开发UI篇—实现UItableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...
- iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建
iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一.实现效果 说明:该示例在storyboard中使用动态单元格来完成. 二.实现 1.项目文件结构 ...
- iOS开发UI篇—控制器的创建
iOS开发UI篇—控制器的创建 说明:控制器有三种创建方式,下面一一进行说明. 一.第一种创建方式(使用代码直接创建) 1.创建一个空的IOS项目. 2.为项目添加一个控制器类. 3.直接在代理方法中 ...
- iOS开发UI篇—控制器的View的创建
iOS开发UI篇—控制器的View的创建 一.6种创建控制器View的方式 #import "NJAppDelegate.h" #import "NJViewContro ...
- [IOS]IOS UI指南
[IOS]IOS UI指南 众所周知,IOS的界面设计,越来越流行,可以说都形成了一个标准,搜集了一些资料,供自己以后学习使用! iOS Human Interface Guidelines (中文翻 ...
- iOS 控制单个控制器旋转
iOS 控制单个控制器旋转 控制单个ViewController 的旋转 //不旋转,保持竖屏 //iOS 5 - (BOOL) shouldAutorotateToInterfaceOrientat ...
- IOS中 什么是UITableView的索引放大镜字符
IOS中 什么是UITableView的索引放大镜字符 [_dataSource addObject:UITableViewIndexSearch]; 版权声明:本文为博主原创文章,未经博主允许不得转 ...
- IOS UI 第八篇:基本UI
实现图片的滚动,并且自动停止在每张图片上 - (void)viewDidLoad{ [super viewDidLoad]; UIScrollView *scrollView = [[U ...
随机推荐
- yum配合rpm查看软件包安装位置
今天安装apache,新版本要求除了apache的安装包以外,还要求先安装apr.apr-util和pcre. 开始并没有急着去下载apr的安装包,而是想看看我的操作系统中有没有安装过了这个软件,结果 ...
- Kali连接不上ssh
1.修改sshd_config文件 vim /etc/ssh/sshd_config 将#PasswordAuthentication yes的注释去掉 将#PermitRootLogin prohi ...
- QtQuickcontrols2控件使用参考
随着Qt的版本升级,其自带的controls控件库也不断升级,目前已经到了2.3的版本.本文通过解读Qt自带的gallery例程,说明新版本controls控件库的相关特性.其具体位置于: 因为相关的 ...
- JavaScript:正则表达式 全局
关于正则表达式的 RegExp方法:test,exec, String 方法:match,search, 全局 g var str = "abababa"; var re = /a ...
- [BZOJ 2200][Usaco2011 Jan]道路和航线 spfa+SLF优化
Description Farmer John正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到T个城镇 (1 <= T <= 25,000),编号为1T.这些城镇之间通过R条 ...
- 论文笔记之:Dynamic Label Propagation for Semi-supervised Multi-class Multi-label Classification ICCV 2013
Dynamic Label Propagation for Semi-supervised Multi-class Multi-label Classification ICCV 2013 在基于Gr ...
- Vue开发中的中央事件总线
在Vue开发中会遇到大量的组件之间共享数据的情形,针对不同的情形,Vue有相对应的解决方案.比如,父组件向子组件传值可以使用props,复杂项目中不同模块之间传值可以使用Vuex.但是,对于一些简单的 ...
- HTTP错误 404.17 - Not Found" IIS 7.5 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理
Errore HTTP 404.2 - Not Found" IIS 7.5 请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理 出现这种情况的原因通常是因为先安装了Framewo ...
- Educational Codeforces Round 1 E. Chocolate Bar dp
题目链接:http://codeforces.com/contest/598/problem/E E. Chocolate Bar time limit per test 2 seconds memo ...
- Jmeter高阶学习,运用NotePad++编写工程,随意复制多个工程到同一个工程
Jmeter创建了工程之后,保存文件后就是一个jmx后缀的文件,你有没有试过单独用文本编辑器打开文件,编辑文件? Step1: 最简单的Jmeter工程,只有一个测试计划 <?xml versi ...