1. UITableView簡單使用
1. UITableView的基本使用
self.tableview.dataSource=self;
self.tableview.delegate=self;
-(NSInteger)numberOfSectionsInTableView: 返回多少組
-numberOfRowsInSection: 返回每組多少行, 有多少組,就調用多少次
-cellForRowAtIndexPath: 返回每組每行的cell, 有多少行,就調用多少次
設置組:
(UITableView *)tableView titleForHeaderInSection: 返回每組的頭部
(UITableView *)tableView titleForFooterInSection: 返回每組的尾部
面板設置: plain: 如果設置seciton footer和header 滾動的時候title有懸浮效果
group: 沒有
2. tableView 和 cell 屬性
self.tableview.rowHeight=100; // 設置行高,每一個cell 行高都是一樣的
// 可以對 每一具體行 設置
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
3. tableView屬性設置:
// 設置分割線顏色
self.tableview.separatorColor = [UIColor redColor];
// 取消分割線
// self.tableview.separatorStyle= UITableViewCellAccessoryNone;
// 設置 tableview允許多選
// self.tableview.allowsMultipleSelection=YES
4. cell 屬性設置:
/*
圖片 、 主題、 內容詳情
* cell.imageview.image= [UIImage imageName:model.icon"
* cell.txtLable.text = model.name
* cell.deatilTextLabel.txt= model.intro
* cell.accesotyType=UITableViewCellAccessoryDisclosureIndicator
* 默認的幾個樣式:
* UITableViewCellStyleDefault: 不顯示 deatilTextLabel
* UITableViewCellStyleValue1:deatilTextLabel 顯示在 textLable 右側
UITableViewCellStyleValue2: imageView不顯示 textLabel 居左
* UITableViewCellStyleSubtitle: 都顯示
*/
UITableViewCell* cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: nil];
// cell 屬性設置
// 設置選中 背景顏色
UIView* bgView=[UIView new];
bgView.backgroundColor=[UIColor blueColor];
cell.selectedBackgroundView= bgView;
代碼效果:
//
// ViewController.m
// UITableView23
//
// Created by 鄧安置 on 2020/5/23.
// Copyright © 2020 鄧安置. All rights reserved.
//
#import "ViewController.h"
@interface ViewController()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableview.dataSource=self;
self.tableview.delegate=self;
// 5. 設置行高,每一個cell 行高都是一樣的
// self.tableview.rowHeight=100;
// 設置分割線顏色
self.tableview.separatorColor = [UIColor redColor];
// 取消分割線
// self.tableview.separatorStyle= UITableViewCellAccessoryNone;
// 設置 tableview允許多選
// self.tableview.allowsMultipleSelection=YES;
}
// 1.返回多少組合 默認返回1組
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
// 2. 每一個組返回多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(section==0){ // 第0組,返回1行
return 1;
}else if(section==1){ // 第一組,返回2行
return 2;
}else {
return 30;
}
// return 2;
}
// 3. 返回cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger seciontIndex= indexPath.section;
/*
圖片 、 主題、 內容詳情
* cell.imageview.image= [UIImage imageName:model.icon"
* cell.txtLable.text = model.name
* cell.deatilTextLabel.txt= model.intro
* cell.accesotyType=UITableViewCellAccessoryDisclosureIndicator
* 默認的幾個樣式:
* UITableViewCellStyleDefault: 不顯示 deatilTextLabel
* UITableViewCellStyleValue1:deatilTextLabel 顯示在 textLable 右側
UITableViewCellStyleValue2: imageView不顯示 textLabel 居左
* UITableViewCellStyleSubtitle: 都顯示
*/
UITableViewCell* cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: nil];
//6. cell 屬性設置
// 設置選中 背景顏色
UIView* bgView=[UIView new];
bgView.backgroundColor=[UIColor blueColor];
cell.selectedBackgroundView= bgView;
if(seciontIndex==0){ // 第0 組
NSInteger rowIndex= indexPath.row; // 第0組第幾行
if(rowIndex==0){
cell.textLabel.text=[NSString stringWithFormat:@"第0組第%ld行",rowIndex];
}
}else if(seciontIndex==1){
// 兩行
NSInteger rowIndex= indexPath.row;
cell.textLabel.text=[NSString stringWithFormat:@"第1組第%ld",rowIndex];
}else{
NSInteger rowIndex= indexPath.row;
cell.textLabel.text=[NSString stringWithFormat:@"第2組第%ld",rowIndex];
}
return cell;
}
//4. 每一組的頭部顯示文本
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString* title=@"";
if(section== 0){
title=@"第一組頭部";
}else if(section==1){
title=@"第二組頭部";
}else{
title=@"第三組頭部";
}
return title;
}
// 每一組的尾部顯示內容
//-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//
//}
// 可以對 每一具體行 設置
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
-(BOOL)prefersStatusBarHidden{
return YES;
}
@end
效果圖:
2. cell 複用, 編輯、刪除、添加
2.1 celll 複用
cell 創建以後放入緩存池中,如果緩存池中有可複用cell, 從緩存池中獲取即可,替換內容
否則重新創建cell
2.2. UITableView編輯
實現右側導航欄功能, 返回重寫 sectionIndexTitlesForTableView 返回 NSArray 即可
tableview cell 修改
tableview cell 插入
cell 刪除
// 打開ios 右側刪除、插入模式
// 打開以後,右側按鈕刪除,如果是滑動刪除,可以關閉
// 但是如果是插入,必須要打開
// self.mytableView.editing=true;
-canEditRowAtIndexPath: 決定哪一行被編輯
-editingStyleForRowAtIndexPath: 返回編輯、還是插入模式
-commitEditingStyle: 點擊編輯、刪除按鈕以後回調,在裏面刪除、插入數據源,刷新UI
-titleForDeleteConfirmationButtonForRowAtIndexPath: 決定刪除文本內容
//
// View2020.m
// UITableView23
//
// Created by 鄧安置 on 2020/5/30.
// Copyright © 2020 鄧安置. All rights reserved.
//
#import "View2020.h"
@interface View2020 ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *mytableView;
// 當前選擇的IndexPath
@property(nonatomic,strong) NSIndexPath* indexPath;
@property(nonatomic,strong) NSMutableDictionary* dict;
@property(nonatomic,copy) NSString* textFileContext;
@end
@implementation View2020
-(NSMutableDictionary*)dict{
if(_dict == nil){
_dict=[NSMutableDictionary new];
NSMutableArray* array1=[NSMutableArray new];
for (int i=0; i<10; i++) {
[array1 addObject:[NSString stringWithFormat:@"A:%d",i]];
}
_dict[@"A"]= array1;
NSMutableArray* array2=[NSMutableArray new];
for (int i=0; i<10; i++) {
[array2 addObject:[NSString stringWithFormat:@"B:%d",i]];
}
_dict[@"B"]= array2;
NSMutableArray* array3=[NSMutableArray new];
for (int i=0; i<10; i++) {
[array3 addObject:[NSString stringWithFormat:@"C:%d",i]];
}
_dict[@"C"]= array3;
}
return _dict;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.mytableView.delegate=self;
self.mytableView.dataSource=self;
// 打開ios 右側刪除、插入模式
// 打開以後,右側按鈕刪除,如果是滑動刪除,可以關閉
// 但是如果是插入,必須要打開
// self.mytableView.editing=true;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSMutableArray* array=nil;
if(section==0){
array= self.dict[@"A"];
return array.count;
}else if(section==1){
array= self.dict[@"B"];
return array.count;
}else{
array= self.dict[@"C"];
return array.count;
}
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* identifier=@"cellId";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(cell == nil){
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier];
}
// cell.textLabel.text=[NSString stringWithFormat:@"欄目%ld",indexPath.row];
NSInteger section = indexPath.section;
NSInteger row = indexPath.row;
if(section==0){
NSMutableArray* array = self.dict[@"A"];
cell.textLabel.text= array[row];
}else if(section==1){
NSMutableArray* array = self.dict[@"B"];
cell.textLabel.text= array[row];
}else{
NSMutableArray* array = self.dict[@"C"];
cell.textLabel.text= array[row];
}
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
if(section==0){
return @"A";
}else if(section==1){
return @"B";
}else{
return @"C";
}
}
/**
實現右側導航欄功能, 返回重寫 sectionIndexTitlesForTableView 返回 NSArray 即可
*/
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
// return self.cehuaArray;
return self.dict.allKeys;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"當一行被取消選中的時候調用");
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIAlertController* alertController= [UIAlertController alertControllerWithTitle:@"標題" message:nil preferredStyle:UIAlertControllerStyleAlert];
// 添加 取消按鈕
UIAlertAction* cancleAction= [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:cancleAction];
//UIAlertActionStyleDefault
// UIAlertActionStyleDestructive : 按鈕是紅色的
// 確定按鈕
UIAlertAction* sureAction= [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
// 直接刷新
// [self.mytableView reloadData];
// 取出編輯的UITextField
UITextField* textField= [alertController textFields][0];
NSLog(@"context=%@",textField.text);
// 動畫刷新
NSIndexPath* path= self.indexPath;
[self.mytableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationFade];
}];
[alertController addAction:sureAction];
// 添加文本框按鈕
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder=@"輸入修改內容";
textField.tag= indexPath.row; //保存行
self.indexPath= indexPath;
// 監聽通知內容發生變化
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertViewTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
}];
//3. 顯示
[self presentViewController:alertController animated:YES completion:^{
}];
}
-(void)alertViewTextFieldDidChange:(NSNotification *)notification
{
UITextField *textField = notification.object;
NSString* text= textField.text;
// NSLog(@"輸入內容是:%@",text);
self.textFileContext= text;
// 修改數據源
NSInteger row= textField.tag;
NSMutableDictionary* dict1=self.dict;
if(self.indexPath.section==0){
NSMutableArray* muArray= dict1[@"A"];
muArray[row]= text;
}else if(self.indexPath.section==1){
NSMutableArray* muArray= dict1[@"B"];
muArray[row]= text;
}else {
NSMutableArray* muArray= dict1[@"C"];
muArray[row]= text;
}
// 刷新數據, 在確定對話框中刷新
// [self.mytableView reloadData];
}
// 決定哪一行可以被編輯
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
// 點擊編輯、刪除按鈕以後的回調
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
// 查看編輯模式
if(editingStyle== UITableViewCellEditingStyleDelete){
NSLog(@"delete....");
// 在數據源中把數據刪除
if(indexPath.section==0){
NSMutableArray* array = self.dict[@"A"];
NSLog(@"delete---%ld",indexPath.row);
[array removeObjectAtIndex:indexPath.row];
// 刷新UI
// [self.mytableView reloadData];
// 有動畫效果
[self.mytableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}else if(indexPath.section==1){
}else{
}
// 刷新UI
}else if(editingStyle==UITableViewCellEditingStyleInsert){
NSLog(@"insert");
if(indexPath.section==0){
NSMutableArray* array= self.dict[@"A"];
[array insertObject:@"New" atIndex:indexPath.row];
[self.mytableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
}
}
// 決定刪除文本內容
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
return @"刪除文本";
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
/** 決定編輯模式:
UITableViewCellEditingStyleNone,
UITableViewCellEditingStyleDelete,
UITableViewCellEditingStyleInsert
*/
return UITableViewCellEditingStyleDelete;
}
@end
3. UITableView cell高度不一致, 代碼實現 UITableViewCell
3.1. 通過數據源 確定cell height
3.2. 返回 heightForRowAtIndexPath 每一個cell 高度給tableview
3.3. cellForRowAtIndexPath 設置數據 、 設置frame
View202011.m
//
// View202011.m
// UITableView23
//
// Created by 鄧安置 on 2020/5/31.
// Copyright © 2020 鄧安置. All rights reserved.
//
#import "View202011.h"
#import "tableCell.h"
#import "WeiBo.h"
#import "ContentFrameModel.h"
@interface View202011()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong) NSMutableArray* array;
@end
@implementation View202011
-(NSMutableArray*) array{
if(_array==nil){
_array = [NSMutableArray new];
for (int i=0; i<80; i++) {
WeiBo* weibo= [WeiBo new];
weibo.context= [NSString stringWithFormat:@"內容.內容.內容內容內容內容內容內容內容容內容內容內容內容容內容內容內容內容容內容內容內容內容容內容內容內容內容容內容內容內容v%d",i];
if(i%2==0){
weibo.context= [NSString stringWithFormat:@"心心心心心心%d",i];
}
weibo.hedder=[NSString stringWithFormat:@"頭部:%d",i];
// [_array addObject:weibo];
ContentFrameModel* frameModel = [ContentFrameModel new];
frameModel.weibo= weibo;
[_array addObject:frameModel];
}
}
return _array;
}
- (void)viewDidLoad {
[super viewDidLoad];
// 實例化一個tableView
UITableView* tableView= [[UITableView alloc] initWithFrame:self.view.frame];
// 設置代理
tableView.dataSource=self;
tableView.delegate=self;
// UIView* headerView= [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
// headerView.backgroundColor=[UIColor orangeColor];
// tableView.tableHeaderView= headerView;
//
//
// UIButton* button=[[UIButton alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 30)];
// button.backgroundColor=[UIColor redColor];
// tableView.tableFooterView=button;
// tableView.rowHeight= 200;
[self.view addSubview:tableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* identity=@"cellId";
// UITableViewCell* cell= [tableView dequeueReusableCellWithIdentifier:identity];
tableCell* cell=[tableView dequeueReusableCellWithIdentifier:identity];
if(cell == nil){
cell = [[tableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identity];
}
ContentFrameModel* frameModel= self.array[indexPath.row];
cell.contentFrame = frameModel;
// cell.nameLabel.text=@"hello";
// WeiBo* weibo= self.array[indexPath.row];
// cell.weibo= weibo;
// cell.contextLabel.text= weibo.context;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
// 返回所有cell 的高度, 有多少條數據,調用多少次,
// 最終返回 所有行高 tableview的高度
ContentFrameModel* frameModel= self.array[indexPath.row];
return frameModel.cellHeight;
}
@end
UITableViewCell 自定義:
tableCell.h
#import <UIKit/UIKit.h>
@class WeiBo,ContentFrameModel;
NS_ASSUME_NONNULL_BEGIN
@interface tableCell : UITableViewCell
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UILabel *contextLabel;
@property (nonatomic, weak) UIImageView *iconImage;
//@property (nonatomic,strong) WeiBo* weibo;
@property(nonatomic,strong) ContentFrameModel* contentFrame;
@end
NS_ASSUME_NONNULL_END
tableCell.m
//
// tableCell.m
// UITableView23
//
// Created by 鄧安置 on 2020/5/31.
// Copyright © 2020 鄧安置. All rights reserved.
//
#import "tableCell.h"
#import "WeiBo.h"
#import "ContentFrameModel.h"
@interface tableCell()
@end
@implementation tableCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// logo 圖標
UIImageView* iconImage= [[UIImageView alloc] init];
iconImage.contentMode = UIViewContentModeScaleToFill;
// iconImage.image=[UIImage imageNamed:@"vip"];
// iconImage.frame=CGRectMake(0, 10, 20, 20);
self.iconImage= iconImage;
[self.contentView addSubview:iconImage];
UILabel *nameLabel = [[UILabel alloc] init];
// 自動換行
[nameLabel setTextColor:[UIColor redColor]];
self.nameLabel= nameLabel;
[self.contentView addSubview:nameLabel];
UILabel* contextLabel= [UILabel new];
// contextLabel.frame=CGRectMake(0, 0, self.contentView.frame.size.width, 100);
contextLabel.font= [UIFont systemFontOfSize:15];
contextLabel.numberOfLines = 0;
self.contextLabel=contextLabel;
[self.contentView addSubview:contextLabel];
}
return self;
}
//-(void)setWeibo:(WeiBo *)weibo{
// _weibo= weibo;
// self.nameLabel.text= weibo.hedder;
// self.contextLabel.text= weibo.context;
//
//
// self.nameLabel.frame= CGRectMake(CGRectGetMaxX(self.iconImage.frame)+5, 5, 100, 20);
//
//
// CGRect screenW = [ UIScreen mainScreen ].bounds;
// // NSLog(@"屏幕寬度:%@", NSStringFromCGRect(screenW));
// // 根據文本內容計算, 顯示內容文本框 寬度確定、高度不確定
// CGSize contentMaxSize = CGSizeMake(screenW.size.width, MAXFLOAT);
// NSDictionary* attributesDict= @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
//
// CGSize contentViewSize= [weibo.context boundingRectWithSize:contentMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDict context:nil].size;
//
// // NSLog(@"大小是:%@",NSStringFromCGSize(contentViewSize));
//
// self.contextLabel.frame= CGRectMake(0, CGRectGetMaxY(self.iconImage.frame)+5,
// contentViewSize.width ,
// contentViewSize.height);
// self.contextLabel.backgroundColor=[UIColor blueColor];
// CGFloat maxY = CGRectGetMaxY(self.contextLabel.frame);
// NSLog(@"maxY:%f",maxY);
// }
-(void)setContentFrame:(ContentFrameModel *)contentFrame{
_contentFrame =contentFrame ;
// 設置數據
WeiBo* weibo= contentFrame.weibo;
self.iconImage.image = [UIImage imageNamed:@"vip"];
self.nameLabel.text = weibo.hedder;
self.contextLabel.text = weibo.context;
// 設置frame
self.iconImage.frame = contentFrame.iconImageFrame;
self.nameLabel.frame= contentFrame.nameLabelFrame;
self.contextLabel.frame= contentFrame.contextLabelFrame;
}
@end
根據數據計算 存儲 frame 和 frame高度的 ContentFrameModel
ContentFrameModel.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@class WeiBo;
NS_ASSUME_NONNULL_BEGIN
@interface ContentFrameModel : NSObject
@property(nonatomic,assign,readonly) CGRect iconImageFrame;
@property(nonatomic,assign,readonly) CGRect nameLabelFrame;
@property(nonatomic,assign,readonly) CGRect contextLabelFrame;
// cell 的高度
@property (nonatomic, assign,readonly) CGFloat cellHeight;
@property(nonatomic,strong) WeiBo* weibo;
@end
NS_ASSUME_NONNULL_END
ContentFrameModel.m
//
// ContentFrameModel.m
// UITableView23
//
// Created by 鄧安置 on 2020/5/31.
// Copyright © 2020 鄧安置. All rights reserved.
//
#import "ContentFrameModel.h"
#import "WeiBo.h"
@implementation ContentFrameModel
-(void)setWeibo:(WeiBo *)weibo{
_weibo = weibo;
_iconImageFrame=CGRectMake(0, 10, 20, 20);
// 如果把這個內容也居中
// Y 軸座標可以用 (圖片高度-文本高度)/2
_nameLabelFrame= CGRectMake(CGRectGetMaxX(_iconImageFrame)+5, 5, 100, 20);
CGRect screenW = [ UIScreen mainScreen ].bounds;
// NSLog(@"屏幕寬度:%@", NSStringFromCGRect(screenW));
// 根據文本內容計算, 顯示內容文本框 寬度確定、高度不確定
CGSize contentMaxSize = CGSizeMake(screenW.size.width, MAXFLOAT);
NSDictionary* attributesDict= @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
CGSize contentViewSize= [weibo.context boundingRectWithSize:contentMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDict context:nil].size;
// NSLog(@"大小是:%@",NSStringFromCGSize(contentViewSize));
_contextLabelFrame= CGRectMake(0, CGRectGetMaxY(_iconImageFrame)+5,
contentViewSize.width ,
contentViewSize.height);
CGFloat maxY = CGRectGetMaxY(_contextLabelFrame);
_cellHeight = maxY ;
// NSLog(@"maxY:%f",maxY);
}
@end
WeiBo.h WeiBo.m
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface WeiBo : NSObject
@property(nonatomic,copy) NSString* hedder;
@property(nonatomic,copy) NSString* context;
@end
NS_ASSUME_NONNULL_END
#import "WeiBo.h"
@implementation WeiBo
@end
效果圖:
4. 靜態單元格 UITableViewController
4.1. 拖入 UITableViewController 控件
4.2 設置靜態單元格,設置組
4.3. 選定組設置行 設置行樣式
5. 通過sb 中uitableview 設置 uitableviewcell
拖入UITableView 控制器 設置 UITableViewCell
tableviewcell.m
// 設置分割線參考博文:
/**
https://blog.csdn.net/smallyou113/article/details/52145835?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
*/
#import "tableviewcell.h"
@interface tableviewcell()
@property(nonatomic,weak) UIView *separatorView;
@end
@implementation tableviewcell
- (void)awakeFromNib {
[super awakeFromNib];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
// 1. 使用懶加載創建分割線view,保證一個cell只有一條
-(UIView *)separatorView
{
if (_separatorView == nil) {
UIView *separatorView = [[UIView alloc]init];
self.separatorView = separatorView;
separatorView.backgroundColor = [UIColor redColor];
[self addSubview:separatorView];
}
return _separatorView;
}
//2. 設置數據
-(void)setWeibo:(WeiBo *)weibo{
_weibo =weibo;
self.imageView.image=[UIImage imageNamed:@"vip"];
self.textLabel.text=weibo.hedder;
self.detailTextLabel.text= weibo.context;
}
//3. 繪製 子控件的時候, 添加分割線
-(void)layoutSubviews{
[super layoutSubviews];
[super layoutSubviews];
self.separatorView.frame = CGRectMake(0, self.bounds.size.height-1, self.bounds.size.width, 1);
}
@end
SbTable.m
/**
通過 storyboard 使用 uitableview cell
可以直接在stroryboard中編輯, 註冊id
獲取通過 registerClass
*/
#import "SbTable.h"
#import "tableviewcell.h"
@interface SbTable ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation SbTable
- (void)viewDidLoad {
[super viewDidLoad];
// 註冊cell
[self.tableView registerClass:[tableviewcell class] forCellReuseIdentifier:@"reuseIdentifier"];
self.tableView.separatorStyle= UITableViewCellSeparatorStyleNone;
self.tableView.rowHeight= 100;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 200;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
tableviewcell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
// Configure the cell...
WeiBo* weibo= [WeiBo new];
weibo.hedder=[NSString stringWithFormat:@"header=%ld",indexPath.row];
weibo.context=[NSString stringWithFormat:@"content=%ld",indexPath.row];
cell.weibo =weibo;
return cell;
}
@end
6. 通過xib 自定義 UITableViewCell
Xib 對應 UITableView cell
XibCell.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@class WeiBo;
@interface XibCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *lable1;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property(nonatomic,strong) WeiBo* weibo;
@end
NS_ASSUME_NONNULL_END
XibCell.m
#import "XibCell.h"
#import "WeiBo.h"
@implementation XibCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void)setWeibo:(WeiBo *)weibo{
NSLog(@"內容....%@",weibo.context);
_weibo =weibo ;
self.lable1.text = weibo.hedder;
self.label2.text = weibo.context;
}
@end
XitTable.m
// xib 行高問題 https://www.jianshu.com/p/616f90328ae4
#import "XitTable.h"
#import "XibCell.h"
#import "WeiBo.h"
@interface XitTable ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation XitTable
static NSString* identity=@"cellId";
- (void)viewDidLoad {
[super viewDidLoad];
UINib* nib= [ UINib nibWithNibName:@"MyView" bundle:nil];
[self.mytableview registerNib:nib forCellReuseIdentifier:identity ];
// 實例化一個tableVie
// 設置代理
self.mytableview.dataSource=self;
self.mytableview.delegate=self;
self.mytableview.rowHeight= UITableViewAutomaticDimension;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 40;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
NSInteger rows = indexPath.row;
XibCell * xibCell= [tableView dequeueReusableCellWithIdentifier:identity forIndexPath:indexPath];
// XibCell * xibCell=[tableView dequeueReusableCellWithIdentifier:identity];
// if(xibCell==nil){
// xibCell = [[[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil] lastObject];
// }
WeiBo* weibo= [WeiBo new];
weibo.hedder=@"header";
weibo.context=[ NSString stringWithFormat:@"context:%ld",rows];
xibCell.weibo =weibo;
return xibCell;
}
@end
源碼地址:
1. 目錄:
ViewController:
uitableview 基本使用
tableview 屬性 、cell屬性設置
設置組
2. View202011:
UITableView cell高度不一致
3. ViewControllerEdit:
cell 複用, 編輯、刪除、添加
4. SbTable:
通過sb 中uitableview 設置 uitableviewcell
5. XitTable
通過xib 設置uitableview cell 問題