創(chuàng)建cell 的 3種方式和cell的一些技巧

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;
    
   }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,908評(píng)論 6 541
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,324評(píng)論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 178,018評(píng)論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,675評(píng)論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,417評(píng)論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,783評(píng)論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,779評(píng)論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,960評(píng)論 0 290
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,522評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,267評(píng)論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,471評(píng)論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,009評(píng)論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,698評(píng)論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,099評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,386評(píng)論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,204評(píng)論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,436評(píng)論 2 378

推薦閱讀更多精彩內(nèi)容