iOs Swift表查看问题

时间:2015-10-28 18:43:52

标签: ios swift

我创建了一个应用程序,该表可以使用多个按钮按钮,可以在行中滚动。 它正在工作,但我注意到当我向下滚动它混合我的价值观。 让我们说在第一行我有3个按钮比在5-6和第六行我也有添加到它的按钮的最后两个信息。我不知道为什么会发生这种情况。当试图打印的数量按钮和它们的位置它给了我一个正确的值,但当我运行它时,另一个抱歉正在发生。

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return roundNumber

}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var cell : RoundWorkoutCell! = tableView.dequeueReusableCellWithIdentifier("Cell") as! RoundWorkoutCell
    if(cell == nil)
    {
        cell = NSBundle.mainBundle().loadNibNamed("Cell", owner: self, options: nil)[0] as! RoundWorkoutCell;
    }
    let exerviseName = RoundLabels[indexPath.row]

    if(indexPath.row == 0){

        for countMe in 0..<self.round_1_exercises.count {
            if(countMe<1){
                roundPosition1.append(5)
            }else{
                roundPosition1.append(115+roundPosition1[countMe-1])
            }
            scrollerSize = 115+roundPosition1[countMe]
        }

    }else if(indexPath.row == 1){
        for countMe in 0..<self.round_2_exercises.count {
            if(countMe<1){
                roundPosition2.append(5)
            }else{
                roundPosition2.append(115+roundPosition2[countMe-1])
            }
            scrollerSize = 115+roundPosition2[countMe]
        }
    }else if(indexPath.row == 2){
        for countMe in 0..<self.round_3_exercises.count {
            if(countMe<1){
                roundPosition3.append(5)
            }else{
                roundPosition3.append(115+roundPosition3[countMe-1])
            }
            scrollerSize = 115+roundPosition3[countMe]
        }
    }else if(indexPath.row == 3){
        for countMe in 0..<self.round_4_exercises.count {
            if(countMe<1){
                roundPosition4.append(5)
            }else{
                roundPosition4.append(115+roundPosition4[countMe-1])
            }
            scrollerSize = 115+roundPosition4[countMe]
        }
    }else if(indexPath.row == 4){
        for countMe in 0..<self.round_5_exercises.count {
            if(countMe<1){
                roundPosition5.append(5)
            }else{
                roundPosition5.append(115+roundPosition5[countMe-1])
            }
            scrollerSize = 115+roundPosition5[countMe]
        }
    }else if(indexPath.row == 5){
        for countMe in 0..<self.round_6_exercises.count {
            if(countMe<1){
                roundPosition6.append(5)
            }else{
                roundPosition6.append(115+roundPosition6[countMe-1])
            }
            scrollerSize = 115+roundPosition6[countMe]
        }
    }else if(indexPath.row == 6){
        for countMe in 0..<self.round_7_exercises.count {
            if(countMe<1){
                roundPosition7.append(5)
            }else{
                roundPosition7.append(115+roundPosition7[countMe-1])
            }
            scrollerSize = 115+roundPosition7[countMe]
        }
    }.....




    cell.RoundExercise_Cell_ScrollView.contentSize = CGSizeMake(CGFloat(scrollerSize),115)
    cell.RoundExercise_Cell_ScrollView.showsHorizontalScrollIndicator = true
    cell.RoundExercise_Cell_ScrollView.indicatorStyle = .Default


    if(indexPath.row==0){
        for index in 0..<self.round_1_exercises.count {
            print("Round position 1 \(self.roundPosition1[index])")

            var imageView : UIImageView
            imageView  = UIImageView(frame:CGRect(x:roundPosition1[index],y: 5, width:110, height: 110 ))
            imageView.backgroundColor = UIColor.whiteColor()
            cell.RoundExercise_Cell_ScrollView.addSubview(imageView)

            let label1: UILabel = UILabel()
            label1.frame = CGRect(x:roundPosition1[index],y: 5, width:110, height: 20 )
            label1.textColor = UIColor(red:17/255.0, green: 22/255.0, blue: 40/255.0, alpha:1.0)
            label1.textAlignment = NSTextAlignment.Center
            label1.font = UIFont(name: "OpenSans-CondensedLight", size: 14)
            label1.text = exerciseInfo.exercise_name(self.round_1_exercises[index] as! String)
            cell.RoundExercise_Cell_ScrollView.addSubview(label1)

            let frame1 = CGRect(x:roundPosition1[index]+10,y:25, width:90, height: 90 )
            let button = UIButton(frame: frame1)
            button.backgroundColor = UIColor.redColor()
            button.setBackgroundImage(UIImage(named: (self.round_1_exercises[index] as? String)!) as UIImage?, forState: .Normal)
            button.setTitleColor(UIColor.blackColor(), forState: .Normal)
            button.setTitle(self.round_1_exercises[index] as? String, forState: .Normal)
            button.setTitleColor(UIColor(red:0/255,green:0/255,blue:0/255,alpha:0.0), forState: .Normal)
            button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
            cell.RoundExercise_Cell_ScrollView.addSubview(button)

            let label: UILabel = UILabel()
            label.frame = CGRect(x:roundPosition1[index],y: 115, width:110, height: 20 )
            label.font = UIFont(name: "OpenSans", size: 14)
            label.textColor = UIColor.whiteColor()
            label.textAlignment = NSTextAlignment.Center
            label.text = self.round_1_decoration[index] as? String
            cell.RoundExercise_Cell_ScrollView.addSubview(label)

        }

    }else if(indexPath.row == 1){
        for index in 0..<self.round_2_exercises.count {
             print("Round position 2 \(self.roundPosition2[index])")

            var imageView : UIImageView
            imageView  = UIImageView(frame:CGRect(x:roundPosition2[index],y: 5, width:110, height: 110 ))
            imageView.backgroundColor = UIColor.whiteColor()
            cell.RoundExercise_Cell_ScrollView.addSubview(imageView)

            let label1: UILabel = UILabel()
            label1.frame = CGRect(x:roundPosition2[index],y: 5, width:110, height: 20 )
            label1.textColor = UIColor(red:17/255.0, green: 22/255.0, blue: 40/255.0, alpha:1.0)
            label1.textAlignment = NSTextAlignment.Center
            label1.font = UIFont(name: "OpenSans-CondensedLight", size: 14)
            label1.text = exerciseInfo.exercise_name(self.round_2_exercises[index] as! String)
            cell.RoundExercise_Cell_ScrollView.addSubview(label1)

            let frame1 = CGRect(x:roundPosition2[index]+10,y:25, width:90, height: 90 )
            let button = UIButton(frame: frame1)
            button.backgroundColor = UIColor.redColor()
            button.setBackgroundImage(UIImage(named: (self.round_2_exercises[index] as? String)!) as UIImage?, forState: .Normal)
            button.setTitleColor(UIColor.blackColor(), forState: .Normal)
            button.setTitle(self.round_2_exercises[index] as? String, forState: .Normal)
            button.setTitleColor(UIColor(red:0/255,green:0/255,blue:0/255,alpha:0.0), forState: .Normal)
            button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
            cell.RoundExercise_Cell_ScrollView.addSubview(button)

            let label: UILabel = UILabel()
            label.frame = CGRect(x:roundPosition2[index],y: 115, width:110, height: 20 )
            label.font = UIFont(name: "OpenSans", size: 14)
            label.textColor = UIColor.whiteColor()
            label.textAlignment = NSTextAlignment.Center
            label.text = self.round_2_decoration[index] as? String
            cell.RoundExercise_Cell_ScrollView.addSubview(label)

        }
    }else if(indexPath.row == 2){
        for index in 0..<self.round_3_exercises.count {
             print("Round position 3 \(self.roundPosition3[index])")

            var imageView : UIImageView
            imageView  = UIImageView(frame:CGRect(x:roundPosition3[index],y: 5, width:110, height: 110 ))
            imageView.backgroundColor = UIColor.whiteColor()
            cell.RoundExercise_Cell_ScrollView.addSubview(imageView)

            let label1: UILabel = UILabel()
            label1.frame = CGRect(x:roundPosition3[index],y: 5, width:110, height: 20 )
            label1.textColor = UIColor(red:17/255.0, green: 22/255.0, blue: 40/255.0, alpha:1.0)
            label1.textAlignment = NSTextAlignment.Center
            label1.font = UIFont(name: "OpenSans-CondensedLight", size: 14)
            label1.text = exerciseInfo.exercise_name(self.round_3_exercises[index] as! String)
            cell.RoundExercise_Cell_ScrollView.addSubview(label1)

            let frame1 = CGRect(x:roundPosition3[index]+10,y:25, width:90, height: 90 )
            let button = UIButton(frame: frame1)
            button.backgroundColor = UIColor.redColor()
            button.setBackgroundImage(UIImage(named: (self.round_3_exercises[index] as? String)!) as UIImage?, forState: .Normal)
            button.setTitleColor(UIColor.blackColor(), forState: .Normal)
            button.setTitle(self.round_3_exercises[index] as? String, forState: .Normal)
            button.setTitleColor(UIColor(red:0/255,green:0/255,blue:0/255,alpha:0.0), forState: .Normal)
            button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
            cell.RoundExercise_Cell_ScrollView.addSubview(button)

            let label: UILabel = UILabel()
            label.frame = CGRect(x:roundPosition3[index],y: 115, width:110, height: 20 )
            label.font = UIFont(name: "OpenSans", size: 14)
            label.textColor = UIColor.whiteColor()
            label.textAlignment = NSTextAlignment.Center
            label.text = self.round_3_decoration[index] as? String
            cell.RoundExercise_Cell_ScrollView.addSubview(label)

        }
    }else if(indexPath.row == 3){
        for index in 0..<self.round_4_exercises.count {
             print("Round position 4 \(self.roundPosition4[index])")

            var imageView : UIImageView
            imageView  = UIImageView(frame:CGRect(x:roundPosition4[index],y: 5, width:110, height: 110 ))
            imageView.backgroundColor = UIColor.whiteColor()
            cell.RoundExercise_Cell_ScrollView.addSubview(imageView)

            let label1: UILabel = UILabel()
            label1.frame = CGRect(x:roundPosition4[index],y: 5, width:110, height: 20 )
            label1.textColor = UIColor(red:17/255.0, green: 22/255.0, blue: 40/255.0, alpha:1.0)
            label1.textAlignment = NSTextAlignment.Center
            label1.font = UIFont(name: "OpenSans-CondensedLight", size: 14)
            label1.text = exerciseInfo.exercise_name(self.round_4_exercises[index] as! String)
            cell.RoundExercise_Cell_ScrollView.addSubview(label1)

            let frame1 = CGRect(x:roundPosition4[index]+10,y:25, width:90, height: 90 )
            let button = UIButton(frame: frame1)
            button.backgroundColor = UIColor.redColor()
            button.setBackgroundImage(UIImage(named: (self.round_4_exercises[index] as? String)!) as UIImage?, forState: .Normal)
            button.setTitleColor(UIColor.blackColor(), forState: .Normal)
            button.setTitle(self.round_4_exercises[index] as? String, forState: .Normal)
            button.setTitleColor(UIColor(red:0/255,green:0/255,blue:0/255,alpha:0.0), forState: .Normal)
            button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
            cell.RoundExercise_Cell_ScrollView.addSubview(button)

            let label: UILabel = UILabel()
            label.frame = CGRect(x:roundPosition4[index],y: 115, width:110, height: 20 )
            label.font = UIFont(name: "OpenSans", size: 14)
            label.textColor = UIColor.whiteColor()
            label.textAlignment = NSTextAlignment.Center
            label.text = self.round_4_decoration[index] as? String
            cell.RoundExercise_Cell_ScrollView.addSubview(label)

        }
    }else if(indexPath.row == 4){
        for index in 0..<self.round_5_exercises.count {
             print("Round position 5 \(self.roundPosition5[index])")

            var imageView : UIImageView
            imageView  = UIImageView(frame:CGRect(x:roundPosition5[index],y: 5, width:110, height: 110 ))
            imageView.backgroundColor = UIColor.whiteColor()
            cell.RoundExercise_Cell_ScrollView.addSubview(imageView)

            let label1: UILabel = UILabel()
            label1.frame = CGRect(x:roundPosition5[index],y: 5, width:110, height: 20 )
            label1.textColor = UIColor(red:17/255.0, green: 22/255.0, blue: 40/255.0, alpha:1.0)
            label1.textAlignment = NSTextAlignment.Center
            label1.font = UIFont(name: "OpenSans-CondensedLight", size: 14)
            label1.text = exerciseInfo.exercise_name(self.round_5_exercises[index] as! String)
            cell.RoundExercise_Cell_ScrollView.addSubview(label1)

            let frame1 = CGRect(x:roundPosition5[index]+10,y:25, width:90, height: 90 )
            let button = UIButton(frame: frame1)
            button.backgroundColor = UIColor.redColor()
            button.setBackgroundImage(UIImage(named: (self.round_5_exercises[index] as? String)!) as UIImage?, forState: .Normal)
            button.setTitleColor(UIColor.blackColor(), forState: .Normal)
            button.setTitle(self.round_5_exercises[index] as? String, forState: .Normal)
            button.setTitleColor(UIColor(red:0/255,green:0/255,blue:0/255,alpha:0.0), forState: .Normal)
            button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
            cell.RoundExercise_Cell_ScrollView.addSubview(button)

            let label: UILabel = UILabel()
            label.frame = CGRect(x:roundPosition5[index],y: 115, width:110, height: 20 )
            label.font = UIFont(name: "OpenSans", size: 14)
            label.textColor = UIColor.whiteColor()
            label.textAlignment = NSTextAlignment.Center
            label.text = self.round_5_decoration[index] as? String
            cell.RoundExercise_Cell_ScrollView.addSubview(label)

        }
    }else if(indexPath.row == 5){
        for index in 0..<self.round_6_exercises.count {
             print("Round position 6 \(self.roundPosition6[index])")

            var imageView : UIImageView
            imageView  = UIImageView(frame:CGRect(x:roundPosition6[index],y: 5, width:110, height: 110 ))
            imageView.backgroundColor = UIColor.whiteColor()
            cell.RoundExercise_Cell_ScrollView.addSubview(imageView)

            let label1: UILabel = UILabel()
            label1.frame = CGRect(x:roundPosition6[index],y: 5, width:110, height: 20 )
            label1.textColor = UIColor(red:17/255.0, green: 22/255.0, blue: 40/255.0, alpha:1.0)
            label1.textAlignment = NSTextAlignment.Center
            label1.font = UIFont(name: "OpenSans-CondensedLight", size: 14)
            label1.text = exerciseInfo.exercise_name(self.round_6_exercises[index] as! String)
            cell.RoundExercise_Cell_ScrollView.addSubview(label1)

            let frame1 = CGRect(x:roundPosition6[index]+10,y:25, width:90, height: 90 )
            let button = UIButton(frame: frame1)
            button.backgroundColor = UIColor.redColor()
            button.setBackgroundImage(UIImage(named: (self.round_6_exercises[index] as? String)!) as UIImage?, forState: .Normal)
            button.setTitleColor(UIColor.blackColor(), forState: .Normal)
            button.setTitle(self.round_6_exercises[index] as? String, forState: .Normal)
            button.setTitleColor(UIColor(red:0/255,green:0/255,blue:0/255,alpha:0.0), forState: .Normal)
            button.addTarget(self, action: "buttonClick:", forControlEvents: .TouchUpInside)
            cell.RoundExercise_Cell_ScrollView.addSubview(button)

            let label: UILabel = UILabel()
            label.frame = CGRect(x:roundPosition6[index],y: 115, width:110, height: 20 )
            label.font = UIFont(name: "OpenSans", size: 14)
            label.textColor = UIColor.whiteColor()
            label.textAlignment = NSTextAlignment.Center
            label.text = self.round_6_decoration[index] as? String
            cell.RoundExercise_Cell_ScrollView.addSubview(label)

        }
    }....


    cell.RoundExercise_Cell_Label.text = exerviseName

    return cell as RoundWorkoutCell
}

func buttonClick(sender:AnyObject){
    exerciseName = sender.currentTitle
    //print(sender.currentTitle)
    self.performSegueWithIdentifier("showExercise", sender:exerciseName)
}

enter image description here

enter image description here

0 个答案:

没有答案