0

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

  1. creating scroll view
  2. creating background view of scores
  3. and creating marks labels as my requirement

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.

  • Can you please post your `tableView(:cellForRow:)` code? – regina_fallangi May 20 '19 at 07:40
  • 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 } – user2389743 May 20 '19 at 11:02
  • Can you *edit* the question to add the code? All the implementations of the `tableView` would be nice. To see how many rows/sections you are presenting. Such amount of code in comments ins not readable. – regina_fallangi May 20 '19 at 11:04
  • Yes, you should edit the question, please. The rest are either comments or answers, there is only one question in each post. – regina_fallangi May 20 '19 at 12:18
  • @regina - i have asked only one question in the post but mentioned clearly point to point .. why u r asking me to edit , am not understanding – user2389743 May 20 '19 at 12:36
  • I think you are probably not understanding what I am saying...I know you asked one question, that's where we are commenting. In StackOverflow, people usually ask to see more relevant code and the people asking just edit the **original and only** question to add the code. All I am asking is to add **all** the methods of the `UITableViewDelegate` and `UITableViewDataSource` that you have implemented. Do you understand now? – regina_fallangi May 20 '19 at 12:54
  • edited @regina. – user2389743 May 20 '19 at 13:38

1 Answers1

1

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.

Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
  • 1
    A few pointers: 1) scroll view is actually not needed within your cell. Table view itself is a scroll view. Your image does not tell me any need for scroll view. 2) You must have cellForRow implementation for data source and access data from some array to make this work. https://developer.apple.com/documentation/uikit/uitableviewdatasource/1614861-tableview The way I see it in your data- you could simply get away with array of dictionaries for every test. Each of these dictionaries will have keys like testdetails, subject1points, subject2 points etc. – Nirav Bhatt May 20 '19 at 11:00