gpt4 book ai didi

ios - 如何修复约束不显示重复的单词?

转载 作者:行者123 更新时间:2023-11-28 07:29:17 25 4
gpt4 key购买 nike

我的 View Controller 在 iPhone XR 上看起来很奇怪。我不知道为什么,但是这些词被复制和覆盖了,但在旧版本上没有。

重复的单词:

IMG:
(点击图片放大)

我实现了一个搜索栏和一个基本的用户名 TableView 。

这是我对标签的约束的屏幕截图。

Constraints

Constraints

swift :

class FindFriendsViewController: UIViewController {

var users = [User]()

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!

var searchItem = [String]()
var searching = false

override func viewDidLoad() {
super.viewDidLoad()

tableView.tableFooterView = UIView()
tableView.rowHeight = 71

let tap = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:)))
tap.cancelsTouchesInView = false
self.view.addGestureRecognizer(tap)
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

UserService.usersExcludingCurrentUser { [unowned self] (users) in
self.users = users

DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}

extension FindFriendsViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searching {
return searchItem.count
} else {
return users.count
}
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FindFriendsCell") as! FindFriendsCell

// let user = users[indexPath.row]

var usernamesArr = [String]()
for user in users {
usernamesArr.append(user.username)
}

if searching {
cell.textLabel?.text = searchItem[indexPath.row]
} else {
cell.textLabel?.text = usernamesArr[indexPath.row]
cell.delegate = self
configure(cell: cell, atIndexPath: indexPath)
}

return cell
}

func configure(cell: FindFriendsCell, atIndexPath indexPath: IndexPath) {
let user = users[indexPath.row]

cell.usernameLabel.text = user.username
cell.followButton.isSelected = user.isFollowed
}
}

extension FindFriendsViewController: FindFriendsCellDelegate {
func didTapFollowButton(_ followButton: UIButton, on cell: FindFriendsCell) {
guard let indexPath = tableView.indexPath(for: cell) else { return
}

followButton.isUserInteractionEnabled = false
let followee = users[indexPath.row]

FollowService.setIsFollowing(!followee.isFollowed, fromCurrentUserTo: followee) { (success) in
defer {
followButton.isUserInteractionEnabled = true
}

guard success else { return }

followee.isFollowed = !followee.isFollowed
self.tableView.reloadRows(at: [indexPath], with: .none)
}
}
}

extension FindFriendsViewController: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
var usernamesArr = [String]()
for user in users {
usernamesArr.append(user.username)
}
searchItem = usernamesArr.filter({$0.lowercased().prefix(searchText.count) == searchText.lowercased()})
searching = true
tableView.reloadData()
}
}

最佳答案

正如@Prashant Tukadiya 所说,textLabelUITableViewCell Outlet,如果您使用的是自定义单元格,则不应使用它。那是你应该做的:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "FindFriendsCell") as! FindFriendsCell

var usernamesArr = [String]()
for user in users {
usernamesArr.append(user.username)
}

if searching {
cell.usernameLabel.text = searchItem[indexPath.row]
} else {
cell.delegate = self
cell.usernameLabel.text = usernamesArr[indexPath.row].username
cell.followButton.isSelected = usernamesArr[indexPath.row].isFollowed
}

return cell
}

关于ios - 如何修复约束不显示重复的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55448159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com