【问题标题】:iOS TableView with Advertisement elements -- IndexPath.row logic带有广告元素的 iOS TableView -- IndexPath.row 逻辑
【发布时间】:2020-12-20 00:20:36
【问题描述】:

我希望在 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
    }
}

【问题讨论】:

    标签: ios swift xcode uitableview indexpath


    【解决方案1】:

    检查索引以显示特定内容是不好的做法。使用您期望的类型更新您的数据源。在这种情况下,使用包含 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()
    }
    

    【讨论】:

    • 好的,这很有趣,但我将如何编码“NewsContentCell”和“AdContentCell”
    • 很多不同的方式来做到这一点。您甚至可以使用您当前的代码并放入开关。我会更新答案
    猜你喜欢
    • 2023-03-31
    • 2021-07-01
    • 1970-01-01
    • 2017-10-04
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多