使用详细视图中的按钮遍历主表视图,但当值大于/小于 # 行 intableview 时,按钮会消失



初学者在这里 - 我有一个TableViewController,可以在DetailView中打印故事。

我已经在我的 DetailView 选项卡栏中成功实现了"上一个"和"下一个"按钮,点击该按钮时,在行 - 1 或行 + 1 当前 indexRow 值处打印故事。我已将"最后一个"按钮设置为在第一行消失,将"下一步"按钮设置为在最后一行消失。

但是,当我在 DetailView 中导航"下一个"和"最后一个"并到达最后一行或第一行时,"最后一个"或"下一个"分别消失,并且它们不会返回。我怀疑这是由于我如何构建 if 语句,并且需要一些帮助来解决这个问题。有人可以告诉我错误吗?

我遇到的另一个相关问题是,如果您选择 TableView 中的第一行或最后一行,DetailView 将加载"最后一个"或"下一个",并且只有当我离开第一个或最后一个故事并返回第一个或最后一个故事时,这些才会消失。解决这个问题的最佳方法是什么?

这是我到目前为止的代码。

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
}
@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" }  nextLabel.title = "Next"}
@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" }lastLabel.title = "Last"} }

但是,如果您隐藏并显示按钮会更好,因为如果按钮具有宽度,则用户仍然可以按下。

如果您仍然遇到困难,请告诉我。

最新更新