2016-12-29 118 views
1

我想将内部类array对象设置为UITableViewCell
这是我的课:Swift:如何将内部类的数组属性设置为UITableViewCells?

import Foundation 
class AvailableCoursesInitialResponseModel: BaseObject { 
    var availableCourses : [CoursesModel]! 
} 

class CoursesModel: BaseObject{ 
    var categoryName : String! 
    var categoryData : [categoryDataModel]! 
} 

class categoryDataModel: BaseObject{ 
    var classInfo : classInfromationModel! 
    var classList : [classListDetailModel]! 
} 

class classInfromationModel: BaseObject{ 
    var displayText : Bool! 
    var updatedBy : Int! 
    var endDate : String! 
    var groups : String! 
} 

class classListDetailModel : BaseObject{ 
    var courseFee: String! 
    var fromTime : NSDate! 
    var courseDate : NSDate! 
    var location : String! 
    var className : String! 
    var registration : String! 
    var courseCalendarId : Int! 
    var toTime : NSDate! 
} 

我做了一个自定义UITableViewCell为:

import UIKit 

class AvailableCoursesSecondViewControllerTableViewCell: UITableViewCell { 

    var model : classListDetailModel! 
    @IBOutlet weak var lblDecription: UILabel! 
    @IBOutlet weak var lblClassName: UILabel! 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 
     // Configure the view for the selected state 
    } 
} 

这是一个包含UITableView视图控制器:

import UIKit 
class AvailableCoursesSecondViewController: 
     UIViewController,UITableViewDelegate{ 
    @IBOutlet weak var tableView: UITableView! 
    var myDataSource = AvailableCoursesInitialResponseModel() 
    override func viewDidLoad(){ 
     self.tableView.reloadData() 
    } 
     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell: AvailableCoursesSecondViewControllerTableViewCell = (NSBundle.mainBundle().loadNibNamed("AvailableCoursesSecondViewControllerTableViewCell", owner: self, options: nil).first as? AvailableCoursesSecondViewControllerTableViewCell)! 
      let model = myDataSource.availableCourses[indexPath.row] 

     Here i want to access the properties of **classListDetailModel** 
     my custom cell. 

     cell.backgroundColor = kColorLightGray_DW 
     return cell 
    } 

如何设置属性classListDetailModel在我的UITableViewCell子类中,或者我如何访问的属性。

回答

1

当你路过呈现视图控制器,那么你可以通过ArrayIndex随着this.what我的意思是,

集这种方法在你的actionMethod

let model = myDataSource.availableCourses[indexPath.row] 
// Instantiate SecondViewController 
       let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController 
    //pass the model to your DataSource 
    secondViewcontroller.myDataSource = model 
    self.navigationController?.pushViewController(secondViewController, animated: true) 

,并在您secondViewController设置你的数据源

var myDataSource : CoursesModel! 

现在你可以设置属性在你的手机里:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell: AvailableCoursesSecondViewControllerTableViewCell = (NSBundle.mainBundle().loadNibNamed("AvailableCoursesSecondViewControllerTableViewCell", owner: self, options: nil).first as? AvailableCoursesSecondViewControllerTableViewCell)! 
      let model = myDataSource.classList[indexPath.row] 
cell.lblClassName.text = model.className 
     cell.lblDescription.text = //set your description from the model. 
    cell.backgroundColor = kColorLightGray_DW 
     return cell 
    } 

我希望你明白我的意思。请随时评论,如果它不工作。

+0

Thanks.It炒作罚款..感谢很多@Niroj – Sam

+0

如果它的工作,那么请接受答案:) – Niroj

2

我不确定您的用户界面以及您处理indexPath的方式,但是您可以使用来自myDataSource.availableCourses[indexPath.row]model访问您的列表项。

搜索它@CoursesModel - >categoryDataModel - >classList财产。

这是classListDetailModelarray的对象,因为像我一开始提到的,我不知道有关indexPath S,它以为自己是引用availableCourses属性,你可以用forEach遍历找项目逐个。

let cell: AvailableCoursesSecondViewControllerTableViewCell = (NSBundle.mainBundle().loadNibNamed("AvailableCoursesSecondViewControllerTableViewCell", owner: self, options: nil).first as? AvailableCoursesSecondViewControllerTableViewCell)! 
let model = myDataSource.availableCourses[indexPath.row] 
// All classList item, as array collection type, at a given indexPath 
let classList: [classListDetailModel] = model.categoryData.classList 
classList.forEach { (classListItem) in 
    // Each classListDetailModel item printed 
    print(classListItem) 
} 
相关问题