【探秘ES6】:JS的第七种基本类型Symbols

更新时间:2015-07-15 10:51:26 点击次数:2273次

这里有一个小问题:当前用户是可以选择EmptyCell的,要解决这个问题需执行以下代码:


def tableView(table_view, shouldHighlightRowAtIndexPath: index_path)
  todays_tasks.any?
end


现在在模拟器上再运行App试试:


rake device_name="iPhone 4s"


在屏幕上还看不到任何新内容,那是因为你还没有添加新内容。但是在模拟器运行时,在Terminal里输入以下内容,就知道虽然屏幕上不显示,但是后台并没有停止运作:

(main)> Task.current
=> <Task: 0xb6a9a60> (entity: Task; id: 0xb63bfa0 <x-coredata://B0AEB5CD-2B77-43BA-B78B-93BA98325BA0/Task/p5> ; data: {
    current = 1;
    name = "Write RubyMotion tutorial";
})

更新label,显示新任务,这些很简单。  在main_view_controller.rb 里定义一个封装方法,是为了给MainView 返回task_name_label 。在tasks_image 下添加:


def task_name_label
  view.task_name_label
end

然后,将这个私有方法插入MainViewController执行的底部:

def set_task_name_label_from_current_task
  if Task.current
    task_name_label_text = Task.current.name
  else
    task_name_label_text = "n/a"
  end
  task_name_label.text = task_name_label_text
end

后,为MainViewController执行viewDidAppear:,然后调用里面的set_task_name_label_from_current_task。记得调用super时,首先执行如下代码:

def viewDidAppear(animated)
  super
  set_task_name_label_from_current_task
end

创建和运行App,凑效了吗?这次选择新任务时,task_name_label会随着任务名称而更新。


编辑任务列表

就快成功了;再添加一样东西,功能就完整了!——删除已经完成的任务——当用户大功告成后,划去列表上的旧任务时,那感觉一定很畅快!

添加这个功能要在tasks_view_controller.rb里执行UITableViewDelegate方法:tableView:canEditRowAtIndexPath:tableView:commitEditingStyle:forRowAtIndexPath:

如下所示:

# 1
def tableView(table_view, canEditRowAtIndexPath: index_path)
  todays_tasks.any?
end  
&nbsp;
# 2
def tableView(table_view, commitEditingStyle:editing_style, forRowAtIndexPath: index_path)
  case editing_style
  when UITableViewCellEditingStyleDelete
    delete_task_at_index(index_path.row)
    if todays_tasks.any?
      tableView.deleteRowsAtIndexPaths([index_path], 
          withRowAnimation: UITableViewRowAnimationFade)
    else
      tableView.reloadRowsAtIndexPaths([index_path], 
          withRowAnimation: UITableViewRowAnimationFade)      
    end
  end
end

来详细阐述一下上面的内容:


  1. 当有任务时,控制器允许编辑任务,tableView:canEditRowAtIndexPath:直接返回true。

  2. 更多用到的是tableView:commitEditingStyle:forRowAtIndexPath,这里详细说明一下:




关闭end之前,在TasksViewController的底部定义delete_task_at_index

def delete_task_at_index(index)
  task = todays_tasks[index]
  task.destroy
  Task.save
  Task.reset_current
end

这个私有方法根据todays_tasks的index标记要划掉的任务,并从数据库中删除,再更新数据库。后一行调用在Task里定义的reset_current方法来清除当前任务。

创建和运行App,试试添加和删除功能:


rake device_name="iPhone 4s"


下一步该做什么?

好啦,你已经成功用RubyMotion App执行了Core Data,是不是高效简洁,一目了然呢?

当然了,有了Core Data和RubyMotion,能做的事情太多了,绝不仅仅止于一个小小的计时器App那么简单。我推荐读者参阅记录在案的完整源代码以及CDQ的READMEdocumentation



本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是一个个人学习交流的平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽,造成漏登,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

回到顶部
嘿,我来帮您!