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

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;
    
   }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末戒劫,一起剝皮案震驚了整個濱河市半夷,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌谱仪,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件否彩,死亡現(xiàn)場離奇詭異疯攒,居然都是意外死亡,警方通過查閱死者的電腦和手機列荔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進店門敬尺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來枚尼,“玉大人,你說我怎么就攤上這事砂吞∈鸹校” “怎么了?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵蜻直,是天一觀的道長盯质。 經(jīng)常有香客問我,道長概而,這世上最難降的妖魔是什么呼巷? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮赎瑰,結(jié)果婚禮上王悍,老公的妹妹穿的比我還像新娘。我一直安慰自己餐曼,他們只是感情好压储,可當(dāng)我...
    茶點故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著源譬,像睡著了一般集惋。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上瓶佳,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天芋膘,我揣著相機與錄音,去河邊找鬼霸饲。 笑死为朋,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的厚脉。 我是一名探鬼主播习寸,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼傻工!你這毒婦竟也來了霞溪?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤中捆,失蹤者是張志新(化名)和其女友劉穎鸯匹,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體泄伪,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡殴蓬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蟋滴。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片染厅。...
    茶點故事閱讀 39,965評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡痘绎,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出肖粮,到底是詐尸還是另有隱情孤页,我是刑警寧澤,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布涩馆,位于F島的核電站行施,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏凌净。R本人自食惡果不足惜悲龟,卻給世界環(huán)境...
    茶點故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望冰寻。 院中可真熱鬧须教,春花似錦、人聲如沸斩芭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽划乖。三九已至贬养,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間琴庵,已是汗流浹背误算。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留迷殿,地道東北人儿礼。 一個月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像庆寺,于是被迫代替她去往敵國和親蚊夫。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,914評論 2 355

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