2016-10-20 70 views
0

我有一个struct /模型,我存储一些JSON数据来填充我的tableview:迅速结构作为函数参数

import SwiftyJSON 

struct MyData { 

    let valOne: Int 
    let valTwo: String 
    let valThree: Int 

    init(json: JSON) { 
     valOne = json["some_data"].intValue 
     valTwo = json["auc_data"].stringValue 
     valThree = json["auc_data"].intValue 
    } 
} 

然后在我的tableview我有我使用一个自定义单元格级的时候我写了我数据:

let data = MyData[indexPath.row] 

let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as! CustomTableViewCell 

cell.configureCellWithData(data) 

我想要做的是通过结构作为参数传递给configureCellWithData(),但我不知道如何申报。

而不是做类似的:

func configureCellWithData(dataOne: Int, dataTwo: String, dataThree: Int) { 

} 

然后

configureCellWithData(data.valOne,data.valTwo,data.valThree) 

我不想有许多参数,而不是我想要发送的结构马上

+1

您是否试过这种方法? func configureCellWithData(data:MyData){} – iMuzahid

+0

'let data = MyData [indexPath.row]'行没有意义。我假设你有一些像'var dataObjects = [MyData]()'或类似的东西定义的属性,然后你可以执行'let data = dataObjects [indexPath.row]'。 – Rob

回答

3

试试这个。

func configureCellWith(data: MyData) { 
    // Do stuff here 
} 
+1

是的,或者如果Swift 3,也许'func configureCell(with data:MyData){...}'。 – Rob