Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 访问非';在UITableView上不可见_Ios_Xcode_Swift_Uitableview_Swift2 - Fatal编程技术网

Ios 访问非';在UITableView上不可见

Ios 访问非';在UITableView上不可见,ios,xcode,swift,uitableview,swift2,Ios,Xcode,Swift,Uitableview,Swift2,我所有的单元格都有一个开关,可以将单元格移动到桌子的底部。它工作得很好,但是当有足够的单元格下降到可见边界以下时,我会收到一个错误,即该单元格不存在。这不起作用,因为我需要更新所有单元格sender.tag func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell: TaskTableViewCell

我所有的单元格都有一个开关,可以将单元格移动到桌子的底部。它工作得很好,但是当有足够的单元格下降到可见边界以下时,我会收到一个错误,即该单元格不存在。这不起作用,因为我需要更新所有单元格
sender.tag

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell: TaskTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! TaskTableViewCell

        var task = tasks[indexPath.row]

        let index = tasks.indexOf(task)!

        cell.cellSwitch.tag = index
        cell.addSubview(cell.cellSwitch)
        cell.tag = index
        cell.cellSwitch.addTarget(self, action: "switched:", forControlEvents: UIControlEvents.TouchUpInside)

    return cell
}

func switched(sender: UIButton) {
let indexPath = NSIndexPath(forRow: sender.tag, inSection: 0)
let finalIndexPath = NSIndexPath(forRow: tasks.count - 1, inSection: 0)

let task = tasks[indexPath.row]
tasks.removeAtIndex(indexPath.row)
tasks.append(task)
self.taskTableView.moveRowAtIndexPath(indexPath, toIndexPath: finalIndexPath)

for var i = 0; i < tasks.count; i ++ {

let cellIndex = NSIndexPath(forRow: i, inSection: 0)
let cell: TaskTableViewCell = self.taskTableView.cellForRowAtIndexPath(cellIndex) as! TaskTableViewCell
cell.cellSwitch.tag = cellIndex.row

}

}

   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cellToBeReturned = UITableViewCell()

    if indexPath.row != tableView.numberOfRowsInSection(0) - 1 {
        let cell: TaskTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as! TaskTableViewCell

        var task = tasks[indexPath.row]

        cell.cellFlatSwitch.removeFromSuperview()
        cell.currentStreak.removeFromSuperview()

        switch(task.icon) {

        case "heart"?:
            task.color = colors[0]
        case "meditate"?:
            task.color  = colors[1]
        case "leaf"?:
            task.color  = colors[2]
        case "run"?:
            task.color  = colors[3]
        case "muscle"?:
            task.color  = colors[4]
        default:
            task.color  = .blackColor()
        }

        switch(task.icon) {
        case "heart"?:
            cell.pretickButton.setImage(Icons.imageOfHeartIcon, forState: .Normal)
        case "meditate"?:
            cell.pretickButton.setImage(Icons.imageOfMeditationIcon, forState: .Normal)
        case "run"?:
            cell.pretickButton.setImage(Icons.imageOfRunIcon, forState: .Normal)
        case "leaf"?:
            cell.pretickButton.setImage(Icons.imageOfLeafIcon, forState: .Normal)
        case "muscle"?:
            cell.pretickButton.setImage(Icons.imageOfMuscleIcon, forState: .Normal)
        default:
            cell.pretickButton.setImage(Icons.imageOfLeafIcon, forState: .Normal)
        }

        if task.longestStreak >= 21 {

            cell.starImage.alpha = 1

        } else {
            cell.starImage.alpha = 0

        }

        if localSettings.habitPreview == "dayCounter" {

            cell.currentStreak = LTMorphingLabel(frame: CGRectMake(11, 29, 39, 31))
            cell.currentStreak.morphingEffect = LTMorphingEffect.Anvil
            cell.currentStreak.textAlignment = NSTextAlignment.Center
            cell.currentStreak.text = String(task.consecutiveDays!)
            cell.addSubview(cell.currentStreak)
            cell.streakSublabel.textAlignment = .Center
            cell.currentStreak.adjustsFontSizeToFitWidth = true
            cell.streakSublabel.adjustsFontSizeToFitWidth = true
            cell.growthPreviewImage.hidden = true
            cell.streakSublabel.hidden = false

        } else {

            if task.growth > 0 && task.growth <= 21 {
                cell.growthPreviewImage.image = UIImage(named: "growth\(task.growth!)")
            } else if task.growth <= 0 {
                cell.growthPreviewImage.image = UIImage(named: "growth1")
            } else if task.growth > 21 {
                cell.growthPreviewImage.image = UIImage(named: "growht21")
            }

            cell.growthPreviewImage.hidden = false
            cell.streakSublabel.hidden = true

        }

        if task.consecutiveDays == 1 {
            cell.streakSublabel.text = "day"
        } else {
            cell.streakSublabel.text = "days"
        }

        cell.taskLabel.adjustsFontSizeToFitWidth = true
        cell.taskLabel.text = task.name
        cell.taskLabel.textColor = task.color
        cell.cellFlatSwitch = AIFlatSwitch(frame: CGRectMake(300, 15, 70, 70))
        cell.cellFlatSwitch.lineWidth = 2.3
        cell.cellFlatSwitch.strokeColor = task.color!
        cell.cellFlatSwitch.trailStrokeColor = task.color!

        let index = tasks.indexOf(task)!

        cell.pretickButton.tag = index
        cell.backgroundColor = UIColor.clearColor()
        cell.addSubview(cell.cellFlatSwitch)
        cell.tag = index

        cell.bringSubviewToFront(cell.pretickButton)
        cell.pretickButton.addTarget(self, action: "switched:", forControlEvents: UIControlEvents.TouchUpInside)
        cell.sendSubviewToBack(cell.cellFlatSwitch)

        if task.doneToday == true {
            cell.cellFlatSwitch.selected = true
            cell.pretickButton.alpha = 0.0101
        } else {
            cell.cellFlatSwitch.setSelected(false, animated: false)
            cell.pretickButton.alpha = 1.0
        }

        cellToBeReturned = cell

    } else {

        let cell: AddNewTableViewCell = tableView.dequeueReusableCellWithIdentifier("add") as! AddNewTableViewCell
        cell.tag = indexPath.row

        cellToBeReturned = cell
    }

    return cellToBeReturned
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
将单元格:TaskTableViewCell=tableView.dequeueReusableCellWithIdentifier(“单元格”)设为!TaskTableViewCell
var task=tasks[indexPath.row]
让index=tasks.indexOf(task)!
cell.cellSwitch.tag=索引
cell.addSubview(cell.cellSwitch)
cell.tag=索引
cell.cellSwitch.addTarget(self,action:“switched:”,forControlEvents:UIControlEvents.TouchUpInside)
返回单元
}
功能切换(发送方:UIButton){
让indepath=nsindepath(forRow:sender.tag,第0节)
让finalIndexPath=nsindepath(forRow:tasks.count-1,第0节)
let task=tasks[indexPath.row]
tasks.removatendex(indexPath.row)
tasks.append(任务)
self.taskTableView.MoveRowatineXpath(indexPath,toIndexPath:finalIndexPath)
对于变量i=0;iUITableView单元格{
var celltobereturn=UITableViewCell()
如果indexath.row!=tableView.numberOfRowsInSection(0)-1{
将单元格:TaskTableViewCell=tableView.dequeueReusableCellWithIdentifier(“单元格”)设为!TaskTableViewCell
var task=tasks[indexPath.row]
cell.cellFlatSwitch.removeFromSuperview()
cell.currentStreak.removeFromSuperview()
开关(task.icon){
案例“心脏”?:
task.color=颜色[0]
案例“冥想”?:
task.color=颜色[1]
“叶子”一案
task.color=颜色[2]
案例“运行”?:
task.color=颜色[3]
“肌肉”一案:
task.color=颜色[4]
违约:
task.color=.blackColor()
}
开关(task.icon){
案例“心脏”?:
cell.pretickButton.setImage(Icons.imageOfHeartIcon,for状态:。正常)
案例“冥想”?:
cell.pretickButton.setImage(Icons.ImageOfEditionIcon,for状态:。正常)
案例“运行”?:
cell.pretickButton.setImage(Icons.imageofrucon,forState:.Normal)
“叶子”一案
cell.pretickButton.setImage(Icons.imageOfLeafIcon,forState:.Normal)
“肌肉”一案:
cell.pretickButton.setImage(Icons.imageOfMuscleIcon,forState:.Normal)
违约:
cell.pretickButton.setImage(Icons.imageOfLeafIcon,forState:.Normal)
}
如果task.longestreak>=21{
cell.starImage.alpha=1
}否则{
cell.starImage.alpha=0
}
如果localSettings.habitPreview==“dayCounter”{
cell.currentStreak=LTMorphingLabel(帧:CGRectMake(11,29,39,31))
cell.currentStreak.morphingEffect=LTMorphingEffect.Anvil
cell.currentStreak.textAlignment=NSTextAlignment.Center
cell.currentStreak.text=字符串(task.continuedDays!)
cell.addSubview(cell.currentStreak)
cell.streakSublabel.textAlignment=.Center
cell.currentStreak.adjustsFontSizeToFitWidth=true
cell.streakSublabel.adjustsFontSizeToFitWidth=true
cell.growthPreviewImage.hidden=true
cell.streakSublabel.hidden=false
}否则{

如果task.growth>0&&task.growth您做错了

不要使用
nsindepath
对象引用调用方法来移动单元格。
UITableView
的实现建议您使用一个数据源(可以是数组、字典、自定义
NSObject
NSFetchedResultsController
等)要实现所有相关的
UITableViewDataSource
UITableViewDelegate
委派方法,您需要刷新您的
UITableView
时,首先对数据源进行更新,然后调用
reloadData
beginUpdates
endUpdates
视图

在您的情况下,我只需使用2个
NSMutableArray
,一个用于带标记的项目,一个用于不带标记的项目。您的开关处理程序应该从单元格索引路径检索项目,并将其从一个数组移到另一个数组。然后您可以调用
BeginUpdate
,并适当地移动单元格

它们的关键是首先以与
UITableViewDataSource
方法一致的方式对数据源进行更改,尤其是
cellForAtIndexPath
方法。基本上,您得到的错误最有可能是因为您的数据源与可见单元格不同步


希望这有帮助。

你做错了

不要使用
nsindepath
对象引用调用方法来移动单元格。
UITableView
的实现建议您使用一个数据源(可以是数组、字典、自定义
NSObject
NSFetchedResultsController
等)要实现所有相关的
UITableViewDataSource
UITableViewDelegate
委派方法,您需要刷新您的
UITableView
时,首先对数据源进行更新,然后调用
reloadData
beginUpdates
endUpdates
视图

在您的情况下,我只需使用2个
NSMutableArray
,一个用于带有的项目,一个用于