初学者 - 我有一个 TableViewController,它在 DetailView 中打印一个故事。
我已经在我的 DetailView 选项卡栏中成功实现了 Last 和 Next 按钮,点击它们时,会在第 1 行或第 + 1 行当前 indexRow 值处打印故事。我已将 Last 按钮设置为在第一行消失,将 Next 按钮设置为在最后一行消失。
但是,当我在 DetailView 中导航 Next 和 Last 并到达最后一行或第一行时,Last 或 Next 分别消失,并且它们不会返回。我怀疑这是由于我如何构建 if 语句并需要一些帮助来解决这个问题。有人可以告诉我错误吗?
我遇到的另一个相关问题是,如果您选择 TableView 中的第一行或最后一行,DetailView 会加载 Last 或 Next,并且只有当我离开第一个或最后一个故事并返回到第一个或最后一个故事。解决这个问题的最佳方法是什么?
到目前为止,这是我的代码。
Class DetailViewController: UIViewController: {
var selectedStory : URL!
var selectedIndex: Int!
var stories: [URL] = []
@IBOutlet weak var lastLabel: UIBarButtonItem!
@IBOutlet weak var nextLabel: UIBarButtonItem!
//MARK: - Tab Bar Next/Last Toolbar button actions
@IBAction func lastButton(_ sender: Any) {
if ((self.selectedIndex - 1) < self.stories.count) {
self.selectedIndex = self.selectedIndex - 1
let nextStory = self.stories[self.selectedIndex]
DispatchQueue.global().async {
let nextStoryText = try? String(contentsOf: nextStory)
DispatchQueue.main.async {
self.textView.text = nextStoryText
}
}
let storyTitle = nextStory.deletingPathExtension().lastPathComponent
storyTitleLabel.title = storyTitle
}
if selectedIndex == 0 {
lastLabel.title = ""
}
else {
lastLabel.title = "Last"
}
}
@IBAction func nextButton(_ sender: Any) {
if ((self.selectedIndex + 1) < self.stories.count) {
self.selectedIndex = self.selectedIndex + 1
let nextStory = self.stories[self.selectedIndex]
DispatchQueue.global().async {
let nextStoryText = try? String(contentsOf: nextStory)
DispatchQueue.main.async {
self.textView.text = nextStoryText
}
}
let storyTitle = nextStory.deletingPathExtension().lastPathComponent
storyTitleLabel.title = storyTitle
}
if ((self.selectedIndex + 1) == self.stories.count) {
nextLabel.title = ""
}
else {
nextLabel.title = "Next"
}
}
}
class TableViewController: UITableViewController {
var stories: [URL] = []
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "DetailVC") as? DetailViewController {
vc.selectedStory = stories[indexPath.row]
vc.stories = stories
vc.selectedIndex = indexPath.row
navigationController?.pushViewController(vc, animated: true)
}
}
}
最佳答案
改变用户交互比改变标题更好
@IBAction func lastButton(_ sender: Any) {
if ((self.selectedIndex - 1) < self.stories.count) {
self.selectedIndex = self.selectedIndex - 1
let nextStory = self.stories[self.selectedIndex]
DispatchQueue.global().async {
let nextStoryText = try? String(contentsOf: nextStory)
DispatchQueue.main.async {
self.textView.text = nextStoryText
}
}
let storyTitle = nextStory.deletingPathExtension().lastPathComponent
storyTitleLabel.title = storyTitle
}
changeTheBarButtonUserInteraction()
}
@IBAction func nextButton(_ sender: Any) {
if ((self.selectedIndex + 1) < self.stories.count) {
self.selectedIndex = self.selectedIndex + 1
let nextStory = self.stories[self.selectedIndex]
DispatchQueue.global().async {
let nextStoryText = try? String(contentsOf: nextStory)
DispatchQueue.main.async {
self.textView.text = nextStoryText
}
}
let storyTitle = nextStory.deletingPathExtension().lastPathComponent
storyTitleLabel.title = storyTitle
}
changeTheBarButtonUserInteraction()
}
func changeTheBarButtonUserInteraction(){
lastLabel.isEnabled = selectedIndex != 0 ? true : false
nextLabel.isEnabled = ((self.selectedIndex + 1) == self.stories.count) ? false : true
}
关于ios - 使用 detailview 中的按钮通过 masterTableView,但是当值大于/小于 tableview 中的行数时按钮消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52308031/