专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构

完成被转发微博

  • 复制 HMStatusOriginalCell.xib 改名称为 HMStatusRetweetCell.xib
  • 修改 HMStatusRetweetCell.xib 的内容,添加被转发微博背景,被转发微博文本。效果如下图
  • 关联 被转发微博文本HMStatusCell
  • HMHomeViewController 中注册2中类型的 Cell
    // 注册可重用 cell
    tableView.register(UINib(nibName: "HMStatusOriginalCell", bundle: nil), forCellReuseIdentifier: OriginalCellIdentifier)
    tableView.register(UINib(nibName: "HMStatusRetweetCell", bundle: nil), forCellReuseIdentifier: RetweetCellIdentifier)
    
  • HMStatusViewModel 添加 isRetweetStatus 属性,用于判断是 原创微博 还是 转发微博,并在 init(status: HMStatus) 根据是否有 retweeted_status 来设置 isRetweetStatus 的值

    /// 一个HMStatusViewModel包含一个微博模型
    class HMStatusViewModel: NSObject {
      // MARK: - 属性
      /// 微博模型
      var status: HMStatus
      ...
    
      /// 是否有被转发微博
      var isRetweetStatus: Bool = false
    
      // MARK: - 构造函数
      init(status: HMStatus) {
          self.status = status
    
          // 设置是否有被转发微博
          isRetweetStatus = status.retweeted_status != nil
          ...
      }
    }
    
  • HMHomeViewControllertableView 数据源方法 tableView:cellForRowAt: 方法,根据不同类型的模型获取不同类型的 Cell

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
      // 获取对应的单个微博VM
      let statusVM = self.statusVMs![indexPath.row]
    
      let cellID = statusVM.isRetweetStatus ? RetweetCellIdentifier : OriginalCellIdentifier
      let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! HMStatusCell
    
      // 设置对应 Cell 的 HMStatusViewModel
      cell.statusVM = statusVM
    
      return cell
    }
    
  • HMStatusCellstatusVM: HMStatusViewModel? 的属性监视器中设置被转发微博的内容

    /// 微博ViewModel
    var statusVM: HMStatusViewModel? {
      didSet {
          guard let status = statusVM?.status else {
              print("statusVM中没有微博")
              return
          }
    
          // 头像,使用SDWebImage设置网络图片内容
          // 用户名称
          // 来源
          // 时间
          // 微博内容
          // 会员等级
          // 认证图标
          ...
    
          // 判断是否是被转发微博
          if statusVM!.isRetweetStatus {
              // 是被转发微博,设置被转发微博文字和图片
              let retweetName = status.retweeted_status?.user?.name ?? "没有名称"
              let retweetText = status.retweeted_status?.text ?? "没有被转发微博"
              retweetStatusText.text = retweetName + ": " + retweetText
              pictureView.pictureURLs = statusVM?.retweetStorePictureURLs
          } else {
              // 设置原创微博图片
              pictureView.pictureURLs = statusVM?.storePictureURLs
          }
      }
    }
    

results matching ""

    No results matching ""