특정 패턴으로 UITableView Custom Cell에 채우려는 2D 배열이 있습니다.
// Parse 백엔드에서 검색
var myArray = [["Name1", "Age1"],["Name2", "Age2"],["Name3", "Age3"]]
// 내가 필요한 것은 :
nameArray = ["Name1", "Name2", "Name3"]
ageArray = ["Age1", "Age2", "Age3]
따라서 indexPath를 사용하여 사용자 정의 UITableView 셀에 Name 데이터를 채울 수 있습니다. 예 : nameArray [indexPath.row]
for in 루프를 사용해 보았습니다.
var nameArray = NSMutableArray()
var ageArray = NSMutableArray()
//Inside CellForRowAtIndexPath
for data in myArray {
self.nameArray.addObject(data[0])
self.ageArray.addObject(data[1])
}
cell.nameLabel.text = "\(nameArray[indexPath.row])"
cell.ageLabel.text = "\(ageArray[indexPath.row])"
하지만 두 셀 모두에 Name1과 Age1로 채워진 반복적 인 이름과 연령 레이블이 있습니다. 누구든지 이것에 무엇이 잘못되었는지 알고 있습니까?
필요에 따라이 데이터를 다시로드하는 더 좋은 방법이 있습니까?
// 업데이트 된 전체 작업 코드 문제 해결에 도움을 준 @ l00phole 덕분에
class NewViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var data = [[String]]()
var cost = Double()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
uploadData()
}
func uploadData() {
let query = PFQuery(className:"Booking")
query.getObjectInBackgroundWithId("X0aRnKMAM2") {
(orders: PFObject?, error: NSError?) -> Void in
if error == nil && orders != nil {
self.data = (orders?.objectForKey("orderDetails"))! as! [[String]]
//[["Vicky","21"],["Luke", "18"],["7253.58"]]
//*****Removing the last element as it is not needed in the tableView data
let count = self.data.count - 1
let c = self.data.removeAtIndex(count)
cost = Double(c[0])!
//******
} else {
print(error)
}
self.reloadTableData()
}
}
func reloadTableData()
{
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
return
})
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return data.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:NewTableViewCell = self.tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NewTableViewCell
// Configure the cell...
cell.nameLabel.text = "\(data[indexPath.row][0])"
cell.ageLabel.text = "\(data[indexPath.row][1])"
return cell
}
당신은에 추가 nameArray
하고 ageArray
때마다 cellForRowAtIndexPath
호출되고 처음을 삭제하지 않습니다. 이것은 비효율적이며 셀을 생성 할 때가 아니라 입력 데이터가 변경 될 때만 해당 배열을 채워야합니다.
나는 당신이 할 수있는 것처럼 그러한 배열이 필요하다고 생각조차하지 않습니다.
cell.nameLabel.text = "\(data[indexPath.row][0])"
cell.ageLabel.text = "\(data[indexPath.row][1])"
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다