2016-07-16 3 views
2

UITableViewCell에서 양식을 만들 때이 양식에 프로토 타입 셀에 lable 및 텍스트 필드가 있습니다. 이미지는 여기 TableView Story board Image입니다. 나는 동적으로 폼을 만드는 식별자를 사용하고, 내 코드는 이제 문제는 내가 18 개 필드를 갖는 오전UITableViewCell 스크롤 후 데이터가 변경됩니다.

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

    let cell = tableView.dequeueReusableCellWithIdentifier("protocell", forIndexPath: indexPath) as! UITableViewCell 

    var lable = cell.viewWithTag(1) as! UILabel 
    lable.text = details[indexPath.row] 
    lable.textColor = UIColor.brownColor().colorWithAlphaComponent(0.7) 

    var textfield = cell.viewWithTag(2) as! UITextField 
    textfield.placeholder = "Enter \(details[indexPath.row])" 
    self.arrayTextField.append(textfield) 
    if details[indexPath.row] == "Name" { 
     textfield.placeholder = "Enter First \(details[indexPath.row]) Middle \(details[indexPath.row]) Last \(details[indexPath.row]) " 
    } 
    textfield.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.2) 

    return cell 
} 

이다 나는 필드에 값을 입력하고 나머지 필드를 채우기 위해 뷰를 스크롤 할 때 필드의 값 변경이 변경됩니다. 도와주세요.

+0

텍스트 필드에 텍스트가있는 데이터 소스를 추적해야합니다. 그래서'tableView : (tableView : cellForRowAtIndexPath :)'에 하나가 있다면 셀을 다시 사용하기 때문에 placeHolder를 넣어야합니다. – Larme

+0

** 테이블 뷰 셀 ('arrayTextField')의 UI 요소를 포함하는 배열을 절대 사용하지 마십시오. 그것은 아마도 당신의 문제의 원인 일 것입니다. (데이터 소스) 모델의 디자인을 개선하고, 모델의 데이터를 변경하고 테이블 뷰를 다시로드하는 것을 고려하십시오. – vadian

+0

는 데이터 소스를 추적합니다. 'arrayTextField' @Larme을 사용하여 데이터 소스에서 어떤 텍스트 필드에 텍스트가 있는지 확인하는 방법을 사용하고 있습니다. 나는 수색했지만 아무것도 발견하지 못했다. – knownUnknown

답변

2

UITableViewCell 하위 클래스를 만들고 prepeareForReuse 함수를 재정 의하여 - 셀을 기본 모드로 설정합니다.

스위프트 :

override func prepareForReuse() { 
    super.prepareForReuse() 

    //set cell to initial state here, reset or set values, etc. 
} 
의견으로 당

-의 방법을 하위 클래스 UITableViewCell :

import UIKit 

class MyCustomCell: UITableViewCell { 
    // things that you can do here: 
    // initialise your properties here. (label, textfield. etc) 
    // layout subviews. 
    // override superclass APIs. 
} 
+0

저는 iPhone 앱 개발의 초보자입니다. 그래서 @Evegeny Karkan이 서브 클래스 생성 방법을 보여줄 수 있습니다. – knownUnknown

+0

@AdnanMomin 내 답변에 대한 업데이트를 참조하십시오. –

0
  1. 배열에있는 텍스트 필드 대신 데이터 소스 "세부 사항"을 보관하지 마십시오 배열을해야 .
  2. 텍스트 필드에 항목을 입력하면 해당 indexPath에 대한 "세부 정보"배열이 업데이트됩니다.
  3. 서브 클래스있는 UITableViewCell과 CustomTableViewCell을하고 레이블을 텍스트 필드에 대한 IBOutlets을 가지고
  4. , 여러분의 인생을 더 쉽게 만드는 코드가 잘
을 캡슐화 그래서 updateWithDetails(details)처럼 CustomTablewViewCell의 방법을 가지고하지 않는 것
+0

내 ** 세부 정보 ** 배열, 필드 레이블을 포함하므로 업데이트 할 수 없습니다. – knownUnknown

+0

그런 다음 배열에서 제거하십시오. 세부 배열에는 EmployeesDetails * employeeDetails와 같은 객체가 있어야하며 array는 employeDetailsArray이어야합니다. 그리고 나서 각 EmployeeDetails는 "정보"를 텍스트 필드가 아닌 String으로 가질 수 있습니다. dataSource는 UI 요소를 별도로 유지하지 않아야합니다. – hariszaman

+0

나의 세부 배열은'details = [ "name", "address", contactnumber ",]'와 같다. – knownUnknown

관련 문제