使用模型填充动态UITableViewCells

时间:2016-01-26 14:58:47

标签: ios swift

到目前为止,在我的应用程序中,我能够使用模型数组填充表行。例如,我将模型定义为NSObject并使用它在加载时初始化数组,然后使用该数组使用cellForRowAtIndexPath填充数据。

这一次,我使用模型创建一个新实体,特别是我使用tableview作为创建新用户的表单,我的模型定义如下:

class NewJob: NSObject {

    var titleId : Int
    var firstName : String
    var lastName : String
    var telNoHome : String
    var telNoWork :String
    var telNoMobile :String
    var email :String
    var street : String
    var propertyNumber : String
    var flatNumber : String
    var town : String
    var county : String
    var locality : String
    var postcode : String
    var countryLookupId : Int
    var workFolderId : String
    var customerAvailibility : String
    var selectedBreadcrumb : String
    var userId : String
    var financeOption : String
    var companyId : String
    var isInitialAppointmentDone : Bool

    // designated initializer for a New Job
    init(titleId : Int,
        firstName: String,
        lastName: String,
        telNoHome : String,
        telNoWork   :String,
        telNoMobile :String,
        email   :String,
        street: String,
        propertyNumber: String,
        flatNumber  :String,
        town    :String,
        county  :String,
        locality    :String,
        postcode    :String,
        countryLookupId :Int,
        workFolderId:   String,
        customerAvailibility:   String,
        selectedBreadcrumb: String,
        userId: String,
        financeOption:  String,
        companyId   :String,
        isInitialAppointmentDone    :Bool
        ) {
            self.titleId = titleId
            self.firstName = firstName
            self.lastName = lastName
            self.telNoHome = telNoHome
            self.telNoMobile = telNoMobile
            self.telNoWork = telNoWork
            self.email = email
            self.street = street
            self.propertyNumber = propertyNumber
            self.flatNumber = flatNumber
            self.town = town
            self.county = county
            self.locality = locality
            self.postcode = postcode
            self.countryLookupId = countryLookupId
            self.workFolderId = workFolderId
            self.customerAvailibility = customerAvailibility
            self.selectedBreadcrumb = selectedBreadcrumb
            self.userId = userId
            self.financeOption = financeOption
            self.companyId = companyId
            self.isInitialAppointmentDone = isInitialAppointmentDone   
     }
}

现在我的UITableView我变得非常困难,因为我不需要一个NewJob数组,我只需要一个NewJob对象,但我需要将NewJob映射到每个UITableView行。

我确实考虑尝试某种类型的数组向导,即从我的新数组中的第一个对象的属性创建一个新数组,但这只会让人感到困惑。

以下是我AddJobController的设置方式:

class AddJobController: UITableViewController {

static let textViewCell = "addJobTextFieldCell"
static let dateCell = "addJobDateCell"

var newJob = [NewJob]()
override func viewDidLoad() {
    super.viewDidLoad()

    self.newJob.append(NewJob(
        titleId : 1,
        firstName : "",
        lastName : "",
        telNoHome : "",
        telNoWork : "",
        telNoMobile : "",
        email : "",
        street : "",
        propertyNumber : "",
        flatNumber : "",
        town : "",
        county : "",
        locality : "",
        postcode : "",
        countryLookupId : 5,
        workFolderId : "",
        customerAvailibility : "",
        selectedBreadcrumb : "",
        userId : "",
        financeOption : "",
        companyId : "",
        isInitialAppointmentDone : true
        ))

    var jobObject = newJob[0]

你可以看到我被jobObject困住的地方,因为我不确定这是解决这个问题的正确方法吗?

0 个答案:

没有答案