iOS 最全的UITableView的各種使用方法
阿新 • • 發佈:2019-02-03
UITableView:
1、重用代理
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
2、定義
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
_tableView.delegate = self; //代理
_tableView.dataSource = self;
[self.view addSubview:_tableView];
[_tableView release];
//分割線顏色
_tableView.separatorColor = [UIColor redColor];
//分割線型別
//_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//行高
_tableView.rowHeight = 100;
//背景顏色
_tableView.backgroundColor = [UIColor orangeColor];
//_tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"2_4.jpg"]];
//背景圖片
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
imageView.image = [UIImage imageNamed:@"2_4.jpg"];
_tableView.backgroundView = imageView;
[imageView release];
3、設定組的行數
想要確定哪一組,則要根據section的值來判斷
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _dataArray.count;
}
4、設定組數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
5、建立行代理
********************
判定組用:indexPath.section
判定行:indexPath.row
********************
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:indexPath.row%2==0 ? @"ID1" : @"ID2"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indexPath.row%2==0 ? @"ID1" : @"ID2"] autorelease];
if (indexPath.row%2==0) {
cell.contentView.backgroundColor = [UIColor redColor];
} else {
cell.contentView.backgroundColor = [UIColor blueColor];
}
}
//cell選中效果
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
NSLog(@"%d",indexPath.row);
cell.textLabel.textColor = [UIColor whiteColor];
return cell;
}
6、
//組標題 頭部
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [NSString stringWithFormat:@"%d",section];
}
//組標題 尾部
- (NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return @"end";
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// indexPath.section
// indexPath.row
if (indexPath.section == 0) {
return 50;
}
return 80;
}
//每一組頭部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;
}
6、設定選中狀態 的閃現: [cell setSelected:NO animated:NO];
//選中時 呼叫的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView viewWithTag:indexPath.section *10 + indexPath.row+1];
// NSLog(@"%@",cell.textLabel.text);
cell.textLabel.textColor = [UIColor blueColor];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[cell setSelected:NO animated:NO];
}
//撤銷選中時,呼叫的方法,可用於取消 accessoryType的狀態,文字的顏色改變等
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView viewWithTag:indexPath.section *10 + indexPath.row+1];
cell.textLabel.textColor = [UIColor blackColor];
cell.accessoryType = UITableViewCellAccessoryNone;
}
7、//加組索引,只有超過一行的時候才能發揮作用
- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [NSArray arrayWithObjects:@"A", @"B" , @"C", @"D", nil];
}
8、 編輯
//編輯按鈕的事件設定
- (void)edit{
[_delArray removeAllObjects];
if (_tableView.editing) {
[_tableView setEditing:NO animated:YES];
} else {
[_tableView setEditing:YES animated:YES];
}
}
//刪除按鈕的事件
- (void)delButtonClick
{
NSMutableIndexSet *index = [NSMutableIndexSet indexSet];
for (NSIndexPath* indexPath in _delArray) {
[index addIndex:indexPath.row];
}
[_dateArray removeObjectsAtIndexes:index];
[_tableView deleteRowsAtIndexPaths:_delArray withRowAnimation:UITableViewRowAnimationAutomatic];
[_delArray removeAllObjects];
}
//允許編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
//指定編輯模式,插入,刪除
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//單插入
//return UITableViewCellEditingStyleInsert ;
//多選刪除
return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
}
//觸發編輯方法;根據editingStyle來判斷時那種型別
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//刪除
if (editingStyle == UITableViewCellEditingStyleDelete) {
//先從陣列刪除
[_dateArray removeObjectAtIndex:indexPath.row];
//再從表格刪除
//刪除行,可以單行,也可以多行
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
if (editingStyle == UITableViewCellEditingStyleInsert) {
//先插入陣列
[_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
//更改刪除按鈕上的文字
- (NSString*)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"刪掉我吧";
}
//編輯時 資料陣列的處理
//多選刪除 選中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.editing) {
[_delArray addObject:indexPath];
}
}
//取消選中
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView.editing) {
[_delArray removeObject:indexPath];
}
}
9、在AppDelegate.m裡面新增UINavigationController,檢視控制
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = nc;
[self.window makeKeyAndVisible];
return YES;
}
10、Cell的三種建立方法
方法1:自己建立 繼承UITableViewCell 的類
#import <UIKit/UIKit.h>
@interface UserCell : UITableViewCell
@property (nonatomic, retain) UILabel* userLabel;
@property (nonatomic, retain) UIImageView* userView;
@end
.m檔案
#import "UserCell.h"
@implementation UserCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self makeView];
}
return self;
}
//自建View
- (void)makeView{
//60
self.userView = [[[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 50, 50)] retain];
[self.contentView addSubview:self.userView];
[self.userView release];
self.userLabel = [[UILabel alloc] initWithFrame:CGRectMake(70, 10, 200, 20)];
[self.contentView addSubview:self.userLabel];
[self.userLabel release];
}
//由於上面property中寫了retain,這裡需要再次釋放,
- (void)dealloc{
// [self.userView release];
// [self.userLabel release];
self.userLabel = nil;
self.userView = nil;
[super dealloc];
}
@end
方法二:用XIB建立
1.建立的時候Class的名字寫和。h檔案一樣的名字
2.Table View Cell裡面的Identifiel屬性 設定成ID,和.m檔案的索引字串一樣
3.在XIB裡面只能建立一個CELL,不能由其他孤立的控制元件,刪除的時候要用 delete鍵刪除,不然不夠乾淨
4.設定 控制元件的Tag值
5.在.m檔案裡面
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"UserCell" owner:self options:nil] lastObject];
}
UILabel* label = (UILabel*)[cell viewWithTag:20];
UIImageView* imageView = (UIImageView*)[cell viewWithTag:10];
label.text = [_dataArray objectAtIndex:indexPath.row];
imageView.image = [UIImage imageNamed:@"1.png"];
return cell;
}
方法三:UserCell 和XIB象結合
1.修改UserCell.h檔案
#import <UIKit/UIKit.h>
@interface UserCell : UITableViewCell
@property (nonatomic, retain) IBOutlet UILabel* userLabel;
@property (nonatomic, retain) IBOutlet UIImageView* userView;
@end
2.在XIB裡面 建立空間的索引:按 右鍵,拖拉線
3..m檔案
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UserCell* cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"UserCell" owner:self options:nil] lastObject];
}
cell.userLabel.text = [_dataArray objectAtIndex:indexPath.row];
cell.userView.image = [UIImage imageNamed:@"1.png"];
return cell;
}
11、 設定行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;`
}
行高自適應:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// 列寬
CGFloat contentWidth = self.tableView.frame.size.width;
// 用何種字型進行顯示
UIFont *font = [UIFont systemFontOfSize:13];
// 該行要顯示的內容
NSString *content = [data objectAtIndex:indexPath.row];
// 計算出顯示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
// 這裡返回需要的高度
return size.height;
}
//contentWidth一般可以直接設定成螢幕寬度,或者一個定值,這樣就簡單了
//系統是先把全部的行高設定完後,才會進行cell的內容設定,所以在設定行高的時候時無法獲取cell的
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
// 列寬
CGFloat contentWidth = self.tableView.frame.size.width;
// 用何種字型進行顯示
UIFont *font = [UIFont systemFontOfSize:13];
// 該行要顯示的內容
NSString *content = [data objectAtIndex:indexPath.row];
// 計算出顯示完內容需要的最小尺寸
CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
// 構建顯示行
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
CGRect rect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0];
// 設定顯示榘形大小
rect.size = size;
// 重置列文字區域
cell.textLabel.frame = rect;
cell.textLabel.text = content;
// 設定自動換行(重要)
cell.textLabel.numberOfLines = 0;
// 設定顯示字型(一定要和之前計算時使用字型一至)
cell.textLabel.font = font;
return cell;
}
12、獲取cell的方法
NSIndexPath *indexPath2 = [NSIndexPath indexPathForItem:indexPath.row inSection:0];
BookCell *cell = (BookCell *)[_tableView cellForRowAtIndexPath:indexPath2]