简体   繁体   English

如何使 class 属性在 swift 中通用,以便可以与多个 model 类型一起使用?

[英]How to make Base class property generic in swift so can use with multiple model type?

I want the property "cellViewModel" as generic so I can reuse BaseCustomCell with a different types of models.我希望将属性“cellViewModel”作为通用属性,以便我可以将 BaseCustomCell 与不同类型的模型一起使用。 Ex.前任。

 struct CELLVIEWMODEL {
   var name: String
   var address: String
 }
 class BaseCustomCell: UITableViewCell {
       var cellViewModel: CELLVIEWMODEL  //should support different model types CELLVIEWMODEL1,CELLVIEWMODEL2
       {
        didSet() {
          setValuesInSubClasses
        }
      }
      func setValuesInSubClasses() {
       //subclass will implement
      }
 }

 class subCell: BaseCustomCell {
     override func setValuesInSubClasses() {
       //set value from cellViewModel
      }
 }

//This is how i am setting from cellForRowAtIndexPath method: //这就是我从 cellForRowAtIndexPath 方法设置的方式:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: viewModel.getCellId(), for: indexPath) as! BaseCustomCell
        cell.cellViewModel = viewModel.getCellModelAtIndexPath(indexPath) //this will set values for subclasses
        return cell
    }

Now, I am creating new BaseCustomCell each time for different types of cellViewModel.现在,我每次都为不同类型的 cellViewModel 创建新的 BaseCustomCell。 Could you help with any solution?你能帮忙解决任何问题吗?

There are several ways to achieve your goals.有几种方法可以实现您的目标。

You can make the actual BaseCustomCell glass generic, but be aware if you use Storyboards, this isn't a solution, since you'd need to hardcode the generic type into the storyboard.您可以使实际的BaseCustomCell玻璃通用,但请注意,如果您使用情节提要,这不是解决方案,因为您需要将通用类型硬编码到 storyboard 中。

The other solution that works with storyboards too is to declare your viewmodel as a protocol, then you can replace it with any concrete implementation of the protocol.另一个适用于故事板的解决方案是将您的视图模型声明为协议,然后您可以将其替换为该协议的任何具体实现。

protocol CellViewModel {
    var name: String { get }
    var address: String { get }
}

class BaseCustomCell: UITableViewCell {
    var cellViewModel: CellViewModel {
        didSet() {
            setValuesInSubClasses
        }
    }
    
    func setValuesInSubClasses() {
       //subclass will implement
    }
}

class SubCell: BaseCustomCell {
     override func setValuesInSubClasses() {
       //set value from cellViewModel
      }
 }

And then your viewModel.getCellModelAtIndexPath should have a return type of CellViewModel , so it can return any type that conforms to the protocol.然后你的viewModel.getCellModelAtIndexPath应该有一个CellViewModel的返回类型,所以它可以返回任何符合协议的类型。

So you simply need to declare your concrete cell view models like class FirstCellViewModel: CellViewModel {... } , etc. and you can return them from getCellModelAtIndexPath所以你只需要声明你的具体单元格视图模型,如class FirstCellViewModel: CellViewModel {... }等,你可以从getCellModelAtIndexPath返回它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM