2016-07-27 5 views
0

버튼을 클릭하면 셀에 대해 tableView.Here를 정의하는 .xib보기가 있습니다. 옵션이 있기 때문에 다른 .xib 파일을 만듭니다. 기본 셀이 없다고 생각합니다. 그래서 어떻게 tableview 셀에 항목을 표시 할 수 있습니다. 나는 .Xib을 이와 같이 등록했고 시도했지만 놀라운 결과를 얻었다..xib에 TableView 추가

class CustomAddOnVC: UIViewController,UITableViewDataSource,UITableViewDelegate { 

let items = ["pk","ak","sk"] 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 
func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return items.count 
} 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let identifier = "Cell" 
    var cell: CustomOneCell! = tableView.dequeueReusableCellWithIdentifier(identifier,forIndexPath: indexPath) as? CustomOneCell 
    if cell == nil { 
     tableView.registerNib(UINib(nibName: "CustomOneCell", bundle: nil), forCellReuseIdentifier: identifier) 
     cell = tableView.dequeueReusableCellWithIdentifier(identifier,forIndexPath:indexPath) as? CustomOneCell 
     cell.addOnName.text = items[indexPath.row] 
     cell.addOnPrice.text = "£0.0" 
    } 

    return cell 
} 

여기에서 계산 된 항목 수는 3 이었지만 데이터는 첫 번째 데이터로만 채워집니다.

스크린 샷

enter image description here

내가 모든 항목의 끝까지 뷰의 높이를 설정하는 방법 한 가지 더? 당신이 품목 후에 여분 공간을 볼 수 있던대로. 크기를 줄이면 다시 같은 문제가 발생합니다. 항목 데이터에 따라 높이를 동적으로 설정하고 싶습니다.

답변

1

처음으로 등록 할 때 더 이상 cell == nil을 입력하지 않았기 때문에 생각합니다. 따라서, 귀하의 업데이트 텍스트를 if 문 외부로 옮기는 것이 좋습니다. 이처럼 : 그래 오

if cell == nil { 
    tableView.registerNib(UINib(nibName: "CustomOneCell", bundle: nil), forCellReuseIdentifier: identifier) 
    cell = tableView.dequeueReusableCellWithIdentifier(identifier,forIndexPath:indexPath) as? CustomOneCell 
} 
cell.addOnName.text = items[indexPath.row] 
cell.addOnPrice.text = "£0.0"` 
+0

.. 덕분에 그 작업 .. –

+0

하지만 어떻게 내가 항목에 따라 화면 크기를 설정할 수 있습니다 ... 예를 들어 나는 하나 개의 데이터가있는 경우 다음 뷰의 크기가되어야한다 테이블 크기 –

+0

자동 레이아웃을 사용하는 경우 제약 조건을 업데이트해야합니다. 어쩌면 새로운 질문을 만들고 어려움을 겪거나 어려움에 직면하게 될 코드를 보여 주어 명확해질 것입니다. –

관련 문제