Reputation: 11
I Am displaying data as shown in this image I want to display the marks or score which I am using as labels. Approx 100s of labels.
I am taking each student details as 1 separate array like:
student1 = [
[test details, test1, test2, test3, test4],
[subject1,100,98,56,78],
[subject2,67,78,89,56],
[subject3,67,56,34,78],
[subject4,56,46,84,38]]
student2 = [[test details, test1, test2, test3, test4],[subject1,100,98,56,78],[subject2,67,78,89,56],[subject3,67,56,34,78],[subject4,56,46,84,38]]
and so on....
these details I want to display in a UITableViewCell
custom cell.
I have written class StudentCustomCell
, which extends UITableViewCell
using for loops
when I execute the code the same labels/marks of only student 1 were displaying in all the cells.
As I am using only student 1 count in for-in-loop.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SCell", for: indexPath) as! StudentCustomCell
cell.cricketerName.text = studentDetails[indexPath.row][0]
cell.studentImage.image = UIImage(named: studentDetails[indexPath.row][0])
}
return cricketerCell
}
func scoreScrollView(){
scView = UIScrollView(frame: CGRect(x: 0, y: 120, width: cricketView.frame.width, height: 220))
scView.backgroundColor = .white
self.addSubview(scView)
var formatView:UIView!
for i in 0..<(student1[0] as AnyObject).count! {
formatView = UIView()
xAxis = 10
formatView.frame = CGRect(x: 0, y: CGFloat(yAxis), width: 640, height: 40)
if(i % 2 == 0){
formatView.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.2132919521)
}else{
formatView.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4010595034)
}
for j in 0..<student1.count {
let label = UILabel()
label.text = "\(student1[j][i])"
label.frame = CGRect(x: xAxis, y: CGFloat(yAxis)+5, width: 60, height: 30)
xAxis = xAxis + 65
scView.addSubview(label)
}
yAxis = yAxis + 40
scView.addSubview(formatView)
scrolls.append(scView)
}
scView.contentSize = CGSize(width: xAxis, height: scView.frame.height)
}
test details subject1 subject2 subject3 subject4
test1 100 67 67 56
test2 98 78 56 46
test3 56 89 34 84
test4 78 56 78 38
Am displaying result like above in the cell/row of table view.
Upvotes: 0
Views: 521
Reputation: 6969
For this kind of complex layout requirements, you should consider serialising your layout logic. Popular way of doing this is to use XIB / Storyboards to layout your UITableViewCell derived cell.
However the challenge is to position elements with respect to changing device sizes. Hence you should make use of auto-layout feature of iOS - just do it once and you will find it much easier to contain additional changes later.
On the contrary - Imagine the horror of writing frame-setting layout logic in code. After 2 weeks, you won't be able to make out why you wrote that code. The valid cases for writing this logic in your code do exist (performance reasons of loading xib/storyboard), but none are applicable here.
For this, see detailed discussion such as this and this. You won't get direct code, but you will surely know how to effectively do it.
Upvotes: 1