2016-08-03 6 views
0

私はUITableViewを複数持っていますUITableViewCell、すべてのセルが異なるデザインと高いものを持っています。問題は私が今tableView(cellForRowAtIndexPath)というメソッドがとても奇妙で見えないように見えて、これが真実であり、ケースの実装。複数のUITableViewCellでtableView(cellForRowAtIndexPath)を読む方法

私の方法:あなたのcellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     if(indexPath.section == 0){ // linked news item 
      let cellIdentifier = "linkedNewsTableViewCell"; 
      let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! LinkedNewsTableViewCell; 
      let linked_news = LinkedNews[indexPath.row]; 
      cell.newTitle.text = linked_news.news_title; 
      return cell; 
     }else if(indexPath.section > 1 && indexPath.row == 0 && indexPath.section != sections.count+2){ // section header item 
      let cellIdentifier = "sectionHeaderTableViewCell"; 
      let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SectionHeaderTableViewCell; 
      let sec = sections[indexPath.section-2]; 
      cell.lblSectionTitle.text = sec.section_name; 
      cell.backgroundColor = Constants.Colors.lightGray; 
      return cell; 
     }else if(indexPath.section == 2+sections.count){ // all rights reserved item 
      let cellIdentifier = "allRightsReservedTableViewCell"; 
      let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! AllRightsReservedTableViewCell; 
      cell.backgroundColor = Constants.Colors.lightGray; 
      return cell; 
     }else if(indexPath.section == 1){ // slider news item 
      let cellIdentifier = "newsTableViewCell"; 
      let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! NewsTableViewCell; 
      cell.imgVideo.hidden = true; 
      let newsItem = SliderNews[indexPath.row]; 
      cell.txtNews.text = newsItem.news_title; 
      cell.lblTime.text = Utilities.timeAgoSinceDate(NSDate(timeIntervalSince1970:newsItem.createdstamp!), numericDates: false); 
      do{ 
      let isSaved = try DBManger.sharedInstance.isSaved(String(newsItem.news_id!)); 
      cell.isSaved = isSaved; 
      cell.news = newsItem; 
       if(isSaved == true){ 
        cell.btnSave.setImage(UIImage(named: "ic_bookmark_blue"), forState: .Normal); 
       }else{ 
        cell.btnSave.setImage(UIImage(named: "ic_bookmark"), forState: .Normal); 
       } 
      }catch{ 

      } 

      if(SliderNews.count-1 == indexPath.row){ 
       cell.buttomLine.hidden = true; 
      }else{ 
       cell.buttomLine.hidden = false; 
      } 

      let image = cell.imgNews.getImage(newsItem.image!, timestamp: String(newsItem.createdstamp!), size: "228", qualty: "70"); 

      cell.imgNews.loadImage(image,contentMode: .ScaleToFill) 

      cell.lblType.text = newsItem.section_name; 
      cell.backgroundColor = Constants.Colors.lightGray; 
      return cell; 
     }else{ // section news item 
      let sec = sections[indexPath.section-2]; 
      if(indexPath.row == sec.news.count+1){ 
       let cellIdentifier = "moreNewsTableViewCell"; 
       let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MoreNewsTableViewCell; 
       cell.lblSectionName.text = "المزيد من \(sec.section_name!)"; 
       cell.backgroundColor = Constants.Colors.lightGray; 
       return cell; 
      } 
      let cellIdentifier = "newsTableViewCell"; 
      let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! NewsTableViewCell; 
      cell.imgVideo.hidden = true; 
      let newsItem = sec.news[indexPath.row-1]; 
      cell.txtNews.text = newsItem.news_title; 
      cell.lblTime.text = Utilities.timeAgoSinceDate(NSDate(timeIntervalSince1970:newsItem.createdstamp!), numericDates: false); 
      cell.lblType.text = sec.section_name; 

      if(sec.news.count == indexPath.row){ 
       cell.buttomLine.hidden = true; 
      }else{ 
       cell.buttomLine.hidden = false; 
      } 

      let image = cell.imgNews.getImage(newsItem.image!, timestamp: String(newsItem.createdstamp!), size: "228", qualty: "70"); 

      cell.imgNews.loadImage(image,contentMode: .ScaleToFill) 

      cell.backgroundColor = Constants.Colors.lightGray; 

      do{ 
       let isSaved = try DBManger.sharedInstance.isSaved(String(newsItem.news_id!)); 
       cell.isSaved = isSaved; 
       cell.news = newsItem; 
       if(isSaved == true){ 
        cell.btnSave.setImage(UIImage(named: "ic_bookmark_blue"), forState: .Normal); 
       }else{ 
        cell.btnSave.setImage(UIImage(named: "ic_bookmark"), forState: .Normal); 
       } 
      }catch{ 

      } 

      return cell; 
     } 

    } 
+0

同じ質問のxD –

+2

使用ファブリックパターンを試してみてくださいcellIdentifier'を呼び出してdoneセルを返すか、Builderパターンを使用して、同じロジックを実装します。私はあなたがデザインパターンを読む必要があると思う:) – iSashok

+0

セルのクラスを設定し、その混乱の代わりにスイッチを使用してください –

答えて

2

あなたのコードで遊んでたら、コードがはるかに優れて見ることができる前に、いくつかの問題はthatsの解決する必要があります。

1.あなたはそれが必要、あなたのtableViewのデータソースを反映したモデルが必要以下のようなものを見て:

let currentSectionModel = self.sections[indexPath.section] 
    let currentRowModel = currentSectionModel[] 

をし、その後、あなたはモデルオブジェクトを設定するために、あなたの細胞を用いて、より汎用的なものを使用することができます。

cell.setRowModel(currentRowModel) 
提示する魔女のセクションを決定

2.あなたのif文は、

for example this line: 

    if indexPath.section > 1 && indexPath.row == 0 && indexPath.section != sections.count+2 { 

あなたは、このためのより良いロジックを見つける必要があり、非常に複雑です。私がセクション1で悲しいようにモデルをorgenizeすると、これははるかに明確に見えるはずで、switch文に変更することができ、enumに質問します。

3.すべてのセルロジックは、セル自体の内部では、より洗練されたviewControllerのために、私は最後の例のコードで行っています。

4.識別子に文字列を使用しないでください。バグが発生する可能性があります。それが私がUITableViewCellの拡張を使用するのを好む理由は、識別子としてクラス名を返します。

セミカラムを使用しないでください。

6.すべてのセルには基本クラスがあります。これにより、セルを返すときに多態性を使用できます。

7.データソースを表すモデルを作成したら、ifステートメントの代わりにSwitchステートメントを使用できます。

これは私が書いたサンプルコードです。コードをコンパイルする前に作業しなければなりません。私はより良い練習例です。 (私は単純な列挙型を使用するには複雑すぎるため、switch文は使用しませんでした。私は悲しいと同じように、あなたは上で動作し、それをよりシンプルにする必要があり何か)

class BaseCellType: UITableViewCell { 
} 

class AllRightsReservedTableViewCell: BaseCellType { 
    // Your implementation 
} 

class LinkedNewsTableViewCell: BaseCellType { 
    func setLinkedNews(linedNews: LinkedNews) { 
     // Your implementation 
    } 
} 

class SectionHeaderTableViewCell: BaseCellType { 
    func setSectionModel(sectionModel: SectionModel) { 
    // Your implementation 
    } 
} 

class MoreNewsTableViewCell: BaseCellType { 
    func setSection(section: SectionModel) { 
    // Your implementation 
    } 
} 

class NewsTableViewCell: BaseCellType { 
    // Your implementation 
} 

class SectionsModel { 
    let rows: [RowModel] 
} 

extension UITableViewCell { 
    static var cellIdentifer: String { 
     get { 
      return String(self.dynamicType).componentsSeparatedByString("__").last! 
     } 
    } 
} 

enum SectionType: Int { 
    case AllRightsReserevedSection = 1, LinkedNewItem = 0 
} 

class ViewController: UIViewController { 
    var sections: [SectionsModel]! 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell: BaseCellType 

     switch (section: indexPath.section,row: indexPath.row) { 
     case (SectionType.AllRightsReserevedSection.rawValue, _): 
      cell = tableView.dequeueReusableCellWithIdentifier(AllRightsReservedTableViewCell.cellIdentifer, forIndexPath: indexPath) as! AllRightsReservedTableViewCell; 
     case (SectionType.LinkedNewItem.rawValue, _): 
      cell = tableView.dequeueReusableCellWithIdentifier(LinkedNewsTableViewCell.cellIdentifer, forIndexPath: indexPath) as! LinkedNewsTableViewCell; 
      cell.setLinkedNews(LinkedNews[indexPath.row]) 
     case let index where index.section > 1 , index.row == 0, index.section != secrion+2: 
      cell = tableView.dequeueReusableCellWithIdentifier(SectionHeaderTableViewCell.cellIdentifer, forIndexPath: indexPath) as! SectionHeaderTableViewCell 
      cell.setSectionModel(sections[indexPath.section-2]) 
     case let index where index.section == section.count + 2: 
      cell = tableView.dequeueReusableCellWithIdentifier(AllRightsReservedTableViewCell.cellIdentifer, forIndexPath: indexPath) as! AllRightsReservedTableViewCell; 
     case let index where index.row == (sec.news.count + 1) : 
      cell = tableView.dequeueReusableCellWithIdentifier(MoreNewsTableViewCell.cellIdentifer, forIndexPath: indexPath) as! MoreNewsTableViewCell; 
      cell.setSection(sections[indexPath.section-2]) 
     default: 
       cell = tableView.dequeueReusableCellWithIdentifier(NewsTableViewCell.cellIdentifer, forIndexPath: indexPath) as! NewsTableViewCell; 
       cell.setNewsItem(sec.news[indexPath.row-1]) 
      } 
     } 
    return cell 
} 
0

- > *のObjective-C

if(your first condition){ 
      static NSString *cellIdentifier = @"cell"; 
      MANewsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
    tableView.rowHeight=30; 
      }else if(second condition){ 
      static NSString *cellIdentifier = @"cell1"; 
      MANewsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
    tableView.rowHeight=40; 
      }else 
    { 
      static NSString *cellIdentifier = @"cell2"; 
      MANewsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 
    tableView.rowHeight=50; 
    } 

私はあなたを助け願っています。

1

I'ts `インタフェースに渡す、この1

func makeBasicTableCell(title:String,details:String,indexPath:NSIndexPath) -> CustomHeaderCell{ 
      let cell = tableProfile.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomHeaderCell 
      cell.titleLable.text = title 
      cell.detLable.text = details 
      return cell 
    } 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     var tblCell: UITableViewCell! 
     tableView.registerNib(UINib(nibName: "profile_details", bundle: nil), forCellReuseIdentifier: "cell") 
     if indexPath.section == 0 { 
      switch(indexPath.row) { 
      case 0: 
       return makeBasicTableCell("Text1", details: "TextDetail1", indexPath: indexPath) 
      case 1: 
       return makeBasicTableCell("Text2", details: "TextDetail2", indexPath: indexPath) 
      case 2: 
       return makeBasicTableCell("Text3", details: "TextDetail3", indexPath: indexPath) 
      case 3: 
       return makeBasicTableCell("Text4", details: "TextDetail4", indexPath: indexPath) 
      default: 
       return makeBasicTableCell("", details: "", indexPath: indexPath) 
      } 
     } else if indexPath.section == 1 { 
      switch(indexPath.row) { 
      case 0: 
       return makeBasicTableCell("Text5", details: "TextDetail5", indexPath: indexPath) 
      case 1: 
       return makeBasicTableCell("Text6", details: "TextDetail6", indexPath: indexPath) 
      case 2: 
       return makeBasicTableCell("Text7", details: "TextDetail7", indexPath: indexPath) 
      default: 
       return makeBasicTableCell("", details: "", indexPath: indexPath) 
      } 
     } 
     return tblCell 
    } 
関連する問題