1慌烧、自定義cell
// 精確刪除一行
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; // 精確刪除一行
2逐抑、怎么把tableview里cell的小對勾的顏色改成別的顏色?
_mTableView.tintColor = [UIColor redColor];
常用 封裝cell 的方法
/**
* 通過一個繼承tableViewcell來創(chuàng)建一個cell
*/
+ (instancetype)cellWithCustonTableViewCell:(UITableView *)tableView;
/**
* 通過一個繼承tableViewcell來創(chuàng)建一個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;
}
- 到時杏死,在控制器直接一行代碼調(diào)用就OK了泵肄,是不是很簡潔呢!
方式一 系統(tǒng)默認(rèn)的淑翼!方法(幾乎不用8病)
- //復(fù)用前和復(fù)用后的區(qū)別?
- 相同點: 顯示上沒有區(qū)別
- 不同點:系統(tǒng)創(chuàng)建的cell的個數(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";
//獲取一個以前用過的cell, 現(xiàn)在沒有用
// 相當(dāng)于從后廚獲取一個碗
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} 方法里添加 自定義的控件 冯丙,并且 給每個 自定義的控件添加上 tag,而已遭京。最后在 :
什么時候注冊cell 胃惜?
給tableView設(shè)置delegate之前注冊cell,即self.tableView.delegate = self; 之前哪雕。
// 注冊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)對應(yīng)的 控件!
- 這種方法是沒有封裝的斯嚎!這里便于理解的原因:(先看完下面代碼利虫,后面會寫帶你封裝:)
-(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;
}
看完了堡僻,上面的代碼是不是很容易糠惫,但是代碼量太多了,而且钉疫,很多都是相同的或相似的硼讽,所以,就必須牲阁,想到封裝起來9谈蟆(額外擴展:我們數(shù)據(jù)解析時看到有些很相似壤躲,那也是想到封裝成一個類 或方法!):例如:
結(jié)構(gòu)相似.png
它們是不是機構(gòu)相似您炉?---想都不用想柒爵,直接封裝成一個類(數(shù)據(jù)模型),很明顯赚爵,一個字典一個模型嘛C拚汀!<较ァ(一個字典里有4個屬性嘛Q渖荨) 或封裝成 一個方法!
-
同理窝剖,我們可以把 cell 封裝在一個類里面麻掸,用繼承自 UITableViewCell,到時 重寫 構(gòu)造方法赐纱!
// 重寫 構(gòu)造方法 (套路來的脊奋!務(wù)必記住8砻琛) -(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ù)必記住F鹨取)
-(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)用時
-(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;
}
- 上面的封裝就可以使代碼簡潔久又!易讀,分工明確效五!
方式 3: XIB 創(chuàng)建cell (較為常用5叵)
-
首先創(chuàng)建一個繼承自 UITableViewCell 的xib 文件。
例如:
xib.png 拖動屬性:
拖動.png
- 在控制器里 注冊N费脉执!
// 注冊表格的cell 的NIB文件
// 告訴編譯器,當(dāng)前的表格的cell 使用的是那個NIB文件
// 只要是用NIB來自定義Cell的,這句話必不可少
[_myTabelView registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
- 調(diào)用時:代碼很簡潔
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * cellid = @"cell";
// 這個xib 定義的 MyTableViewCell
MyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
//這個代碼可以不寫
if (cell == nil) {
//這個代碼可以不寫
// 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;
}