簡體   English   中英

帶有廣告元素的 iOS TableView — IndexPath.row 邏輯

[英]iOS TableView with Advertisement elements — IndexPath.row logic

我希望在 0 索引處以及此后每三個單元格之后添加一個廣告單元格。 我成功添加了第一個,但不確定如何處理。

兩個廣告都只拍攝一張圖片。

我目前的代碼如下:

var adCount = 1
var newsTitleArray : [String] = ["News1"]

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return newsTitleArray.count + adCount
 }
 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BannerTableViewCell") as! BannerTableViewCell
        cell.adImageView.image = UIImage(named:"Logo")
        NewsTableView.rowHeight = 50
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "NewsTableViewCell") as! NewsTableViewCell
        cell.newsTitle.text = newsTitleArray[indexPath.row - 1]
        cell.newsSubTitle.text = newsSubTitleArray[indexPath.row - 1]
        cell.newsDate.text = newsDateArray[indexPath.row - 1]
        cell.newsImageView.image = UIImage(named: randomPicArray[indexPath.row - 1])
        cell.selectionStyle = .none
        NewsTableView.rowHeight = 500
        return cell
    }
}

檢查索引以顯示特定內容是不好的做法。 使用您期望的類型更新您的數據源。 在這種情況下,使用包含 News & Ads 對象的 dataSource 代替 newsTitleArray。

enum ContentType {
  case news
  case ad
}

struct NewsContent {
 let type: ContentType = .news
 let title: String
 //More if needed 
}

struct AdContent {
 let type: ContentType = .ad
 let title: String
 //More if needed 
}

let dataSource = [AdContent, NewsContent, NewsContent, NewsContent, AdContent, ...]

您可以使用此框架來創建多種內容類型。 這也簡化了您訪問數據的方式。 就像在numberOfRowsInSection你不需要做newsContent + ad你可以只返回dataSource.count 這種方法易於閱讀和維護。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return dataSource.count
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
   let content = dataSource[indexPath.row]
   let height: CGFloat?
   switch content.type {
   case .news:
      height = 500
   case .ad:
      height = 50
   }
   return height ?? 500 // return default
}

現在根據內容類型返回/更新單元格

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let content = dataSource[indexPath.row]
   let cell: UITableViewCell?
   switch content.type {
   case .news:
      cell = tableView.dequeueReusableCell(withIdentifier: "NewsTableViewCell") as? NewsTableViewCell
      cell?.newsTitle.text = newsTitleArray[indexPath.row - 1]
      cell?.newsSubTitle.text = newsSubTitleArray[indexPath.row - 1]
      cell?.newsDate.text = newsDateArray[indexPath.row - 1]
      cell?.newsImageView.image = UIImage(named: randomPicArray[indexPath.row - 1])
      cell?.selectionStyle = .none
    case .ad:
      cell = tableView.dequeueReusableCell(withIdentifier: "BannerTableViewCell") as? BannerTableViewCell
      cell?.adImageView.image = UIImage(named:"Logo")
    }

    return cell ?? UITableViewCell()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM