1、自定義cell
// 精確刪除一行
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; // 精確刪除一行
2、怎么把tableview里cell的小對(duì)勾的顏色改成別的顏色?
_mTableView.tintColor = [UIColor redColor];
常用 封裝cell 的方法
/**
* 通過一個(gè)繼承tableViewcell來創(chuàng)建一個(gè)cell
*/
+ (instancetype)cellWithCustonTableViewCell:(UITableView *)tableView;
/**
* 通過一個(gè)繼承tableViewcell來創(chuàng)建一個(gè)cell
*/
+ (instancetype)cellWithCustonTableViewCell:(UITableView *)tableView
{
static NSString *ID = @"cell";
ODDetailCustonCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
// 從xib中加載cell
cell = [[[NSBundle mainBundle] loadNibNamed:@"ODDetailCustonCell" owner:nil options:nil] lastObject];
}
return cell;
}
- 到時(shí),在控制器直接一行代碼調(diào)用就OK了,是不是很簡(jiǎn)潔呢!
方式一 系統(tǒng)默認(rèn)的!方法(幾乎不用!)
- //復(fù)用前和復(fù)用后的區(qū)別?
- 相同點(diǎn): 顯示上沒有區(qū)別
- 不同點(diǎn):系統(tǒng)創(chuàng)建的cell的個(gè)數(shù)有限的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell的標(biāo)示符
// 以后表格中可能有不同樣式的cell, 區(qū)分樣式的cell
static NSString *cellID = @"cell";
//獲取一個(gè)以前用過的cell, 現(xiàn)在沒有用
// 相當(dāng)于從后廚獲取一個(gè)碗
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil) //沒碗可取
{
//參數(shù) initWithStyle設(shè)置風(fēng)格,共有4種
//樣式: Default,Value1,Value2,Subtitle
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID] autorelease];
cell.tag = indexPath.row;
count++;
}
NSLog(@"row=%d,count=%d,tag=%d",indexPath.row,count,cell.tag);
//設(shè)置cell上顯示的數(shù)據(jù)
cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
//設(shè)置單元格右邊的風(fēng)格
cell.accessoryType = UITableViewCellAccessoryCheckmark;
return cell;
}
方式2: 使用自定義 創(chuàng)建 cell (非常常用)
- 同樣在代理方法里寫,只不過 在 if (cell == nil) {XXXXXX} 方法里添加 自定義的控件 ,并且 給每個(gè) 自定義的控件添加上 tag,而已。最后在 :
什么時(shí)候注冊(cè)cell ?
給tableView設(shè)置delegate之前注冊(cè)cell,即self.tableView.delegate = self; 之前。
// 注冊(cè)cell ---
[self.myTableViwe registerNib:[UINib nibWithNibName:@"RecomListCell" bundle:nil] forCellReuseIdentifier:@"RecomListCell"];
if (cell == nil) {XXXXXX} 方法外 用tag匹配,例如這種方式獲取 : UILabel * label = (UILabel *)[cell.contentView viewWithTag:1001];相當(dāng)對(duì)應(yīng)的 控件!
- 這種方法是沒有封裝的!這里便于理解的原因:(先看完下面代碼,后面會(huì)寫帶你封裝:)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * cellid = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
#pragma mark --- 自定義的 控件 ---》 并 給 tag
// 在cell 上自定義添加控件
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 100, 30)];
label.tag = 1001;
[cell.contentView addSubview:label];// 添加到 contentView
// 添加自定義子標(biāo)簽
UILabel * label2 = [[UILabel alloc] initWithFrame:CGRectMake(200, 50, 100, 30)];
label2.tag = 1002;
[cell.contentView addSubview:label2];// 添加到 contentView
}
// 如果需要在cell 上添加自定義的控件
// 需要在if(cell == nil) 的判斷中添加所需控件
// 在判斷外面 通過控件的tag 值查找到該控件,然后設(shè)置需要顯示的內(nèi)容即可
cell.textLabel.text = [_dataArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"002@2x.png"];
UILabel * label = (UILabel *)[cell.contentView viewWithTag:1001];
label.text = [NSString stringWithFormat:@"%d",indexPath.row];
// 子標(biāo)題
UILabel * label2 = (UILabel *)[cell.contentView viewWithTag:1002];
label2.text = @"我是子標(biāo)題!";
return cell;
}
看完了,上面的代碼是不是很容易,但是代碼量太多了,而且,很多都是相同的或相似的,所以,就必須,想到封裝起來?。~外擴(kuò)展:我們數(shù)據(jù)解析時(shí)看到有些很相似,那也是想到封裝成一個(gè)類 或方法?。豪纾?/h6>
結(jié)構(gòu)相似.png
它們是不是機(jī)構(gòu)相似?---想都不用想,直接封裝成一個(gè)類(數(shù)據(jù)模型),很明顯,一個(gè)字典一個(gè)模型嘛?。。。ㄒ粋€(gè)字典里有4個(gè)屬性嘛?。?或封裝成 一個(gè)方法!
-
同理,我們可以把 cell 封裝在一個(gè)類里面,用繼承自 UITableViewCell,到時(shí) 重寫 構(gòu)造方法!
// 重寫 構(gòu)造方法 (套路來的!務(wù)必記住?。?-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
@interface TableViewCell : UITableViewCell
// 繼承 UITableViewCell 并自定義 控件
@property (retain,nonatomic)UILabel * titleLabel;
@property (retain,nonatomic)UIImageView * iconImageView;
@property (retain,nonatomic)UILabel * subTitleLabel;
@end
-
import "TableViewCell.h"
@implementation TableViewCell
// 重寫構(gòu)造方法 -- 只調(diào)用一次嘛 (套路來的!務(wù)必記?。。?-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// qq 聊天 不設(shè)置 大小
_iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 120, 80)];
[self.contentView addSubview:_iconImageView];
// 大標(biāo)題
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 5, 200, 40)];
_titleLabel.textColor = [UIColor redColor];
_titleLabel.font = [UIFont boldSystemFontOfSize:20];
[self.contentView addSubview:_titleLabel];
// 子標(biāo)題
_subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 50, 200, 40)];
_subTitleLabel.textColor = [ UIColor yellowColor];
[self.contentView addSubview:_subTitleLabel];
}
return self;
}
調(diào)用時(shí)
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * cellid = @"cell";
// 是 TableViewCell 不是之前的系統(tǒng)的 UITableViewCell
TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (cell == nil) {
// 是 TableViewCell 不是之前的系統(tǒng)的 UITableViewCell
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}
cell.titleLabel.text = @"我是主標(biāo)題";
cell.subTitleLabel.text = @"我是副標(biāo)題";
cell.iconImageView.image = [UIImage imageNamed:@"guideHome.png"];
return cell;
}
- 上面的封裝就可以使代碼簡(jiǎn)潔!易讀,分工明確!
方式 3: XIB 創(chuàng)建cell (較為常用?。?/h3>
-
首先創(chuàng)建一個(gè)繼承自 UITableViewCell 的xib 文件。
例如:
xib.png
拖動(dòng)屬性:
拖動(dòng).png
- 在控制器里 注冊(cè)!!
// 注冊(cè)表格的cell 的NIB文件
// 告訴編譯器,當(dāng)前的表格的cell 使用的是那個(gè)NIB文件
// 只要是用NIB來自定義Cell的,這句話必不可少
[_myTabelView registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
- 調(diào)用時(shí):代碼很簡(jiǎn)潔
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * cellid = @"cell";
// 這個(gè)xib 定義的 MyTableViewCell
MyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
//這個(gè)代碼可以不寫
if (cell == nil) {
//這個(gè)代碼可以不寫
// cell = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:nil options:nil][0];
}
// 直接調(diào)用就可以啦!
cell.titleLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
cell.IconImageView.image = [UIImage imageNamed:@"guideHome.png"];
cell.priceOff.text = @"200";
cell.priceLabel.text = @"1000";
cell.saleCountLabel.text = @"100232";
cell.addsLabel.text = @"全國";
return cell;
}
結(jié)構(gòu)相似.png
它們是不是機(jī)構(gòu)相似?---想都不用想,直接封裝成一個(gè)類(數(shù)據(jù)模型),很明顯,一個(gè)字典一個(gè)模型嘛?。。。ㄒ粋€(gè)字典里有4個(gè)屬性嘛?。?或封裝成 一個(gè)方法!
同理,我們可以把 cell 封裝在一個(gè)類里面,用繼承自 UITableViewCell,到時(shí) 重寫 構(gòu)造方法!
// 重寫 構(gòu)造方法 (套路來的!務(wù)必記住?。?-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
@interface TableViewCell : UITableViewCell
// 繼承 UITableViewCell 并自定義 控件
@property (retain,nonatomic)UILabel * titleLabel;
@property (retain,nonatomic)UIImageView * iconImageView;
@property (retain,nonatomic)UILabel * subTitleLabel;
@end
import "TableViewCell.h"
@implementation TableViewCell
// 重寫構(gòu)造方法 -- 只調(diào)用一次嘛 (套路來的!務(wù)必記?。。?-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
// qq 聊天 不設(shè)置 大小
_iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 120, 80)];
[self.contentView addSubview:_iconImageView];
// 大標(biāo)題
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 5, 200, 40)];
_titleLabel.textColor = [UIColor redColor];
_titleLabel.font = [UIFont boldSystemFontOfSize:20];
[self.contentView addSubview:_titleLabel];
// 子標(biāo)題
_subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(140, 50, 200, 40)];
_subTitleLabel.textColor = [ UIColor yellowColor];
[self.contentView addSubview:_subTitleLabel];
}
return self;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * cellid = @"cell";
// 是 TableViewCell 不是之前的系統(tǒng)的 UITableViewCell
TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (cell == nil) {
// 是 TableViewCell 不是之前的系統(tǒng)的 UITableViewCell
cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}
cell.titleLabel.text = @"我是主標(biāo)題";
cell.subTitleLabel.text = @"我是副標(biāo)題";
cell.iconImageView.image = [UIImage imageNamed:@"guideHome.png"];
return cell;
}
-
首先創(chuàng)建一個(gè)繼承自 UITableViewCell 的xib 文件。
例如:
xib.png 拖動(dòng)屬性:
拖動(dòng).png
- 在控制器里 注冊(cè)!!
// 注冊(cè)表格的cell 的NIB文件
// 告訴編譯器,當(dāng)前的表格的cell 使用的是那個(gè)NIB文件
// 只要是用NIB來自定義Cell的,這句話必不可少
[_myTabelView registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
- 調(diào)用時(shí):代碼很簡(jiǎn)潔
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * cellid = @"cell";
// 這個(gè)xib 定義的 MyTableViewCell
MyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
//這個(gè)代碼可以不寫
if (cell == nil) {
//這個(gè)代碼可以不寫
// cell = [[NSBundle mainBundle] loadNibNamed:@"MyTableViewCell" owner:nil options:nil][0];
}
// 直接調(diào)用就可以啦!
cell.titleLabel.text = [NSString stringWithFormat:@"%d",indexPath.row];
cell.IconImageView.image = [UIImage imageNamed:@"guideHome.png"];
cell.priceOff.text = @"200";
cell.priceLabel.text = @"1000";
cell.saleCountLabel.text = @"100232";
cell.addsLabel.text = @"全國";
return cell;
}