初学者 - 我有一个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
}
@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"} }
但是如果你隐藏并显示按钮会更好,因为如果按钮有宽度,那么用户仍然可以按下它。
如果您仍然遇到困难,请告诉我。