我有一个UITableView
,包含3个原型单元格和3个自定义单元格类:
FriendCell
FriendRequestCell
和AddFriendCell
。
初始化后,表格显示Friends
。
如果有FriendRequests
,则会在Friends
上方的部分中显示它们。
如果没有FriendRequests
,则只显示Friends
。
但是,我还有一个UISearchBar
来搜索用户,当它有结果时,应该返回AddFriendCell
并重新加载表。
相反,我明白了:
代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if searching == true {
if let cell = tableView.dequeueReusableCellWithIdentifier("AddFriendCell", forIndexPath: indexPath) as? AddFriendCell {
let resultCell = userResults[indexPath.row]
cell.configureCell(resultCell)
}
} else {
if friendRequests.isEmpty || (indexPath.section == 1) {
if let cell = tableView.dequeueReusableCellWithIdentifier("FriendCell", forIndexPath: indexPath) as? FriendCell {
let friendCell = friends[indexPath.row]
cell.configureCell(friendCell)
}
} else {
if (indexPath.section == 0) {
if let cell = tableView.dequeueReusableCellWithIdentifier("FriendRequestCell", forIndexPath: indexPath) as? FriendRequestCell {
let friendRequestCell = friendRequests[indexPath.row]
cell.configureCell(friendRequestCell)
}
}
}
}
return FriendCell()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
if searching == true {
return 1
} else {
return friendsDataSource.count
}
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searching == true {
return userResults.count
} else {
return friendsDataSource[section].count
}
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if searching == true {
return nil
} else {
if friendsDataSource.count > 1 {
if section == 0 {
return "Friend Requests"
} else if section == 1 {
return "Friends"
}
} else {
return "Friends"
}
return "Friends"
}
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if searching == true {
return 0
} else {
return 25
}
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searching = true
searchBar.resignFirstResponder()
userResults = [UserProfile]()
activitySpinner.startAnimating()
if let searchText = searchBar.text {
let userProfileQuery = PFQuery(className: "UserProfile")
userProfileQuery.whereKey("username", containsString: searchText)
userProfileQuery.findObjectsInBackgroundWithBlock({ resultArray, error in
if error != nil {
print("there's been an error searching for users: \(error)")
} else if let resultArray = resultArray {
print("number of results: \(resultArray.count)")
self.parseResults = resultArray
for userProfile in resultArray {
let username = userProfile["username"] as! String
let profilePicUrl = userProfile["profilePicUrl"] as! String
let parseObjectId = userProfile.objectId!
let newProfile = UserProfile(username: username, profilePicUrl: profilePicUrl, parseObjectId: parseObjectId)
self.userResults.append(newProfile)
}
self.tableView.reloadData()
self.activitySpinner.stopAnimating()
}
})
}
}
关于问题根源的任何想法?
答案 0 :(得分:1)
如果方法findObjectsInBackgroundWithBlock
是异步的,我认为您可以使用self.tableView.reloadData()
代替dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
添加self.tableView.rowHeight = 50
并且您不会返回您的addfriendcell。你只需在最后一行返回FriendsCell()。添加返回单元格。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if searching == true {
if let cell = tableView.dequeueReusableCellWithIdentifier("AddFriendCell", forIndexPath: indexPath) as? AddFriendCell {
let resultCell = userResults[indexPath.row]
cell.configureCell(resultCell)
return cell! //add the return
}
} else {
if friendRequests.isEmpty || (indexPath.section == 1) {
if let cell = tableView.dequeueReusableCellWithIdentifier("FriendCell", forIndexPath: indexPath) as? FriendCell {
let friendCell = friends[indexPath.row]
cell.configureCell(friendCell)
return cell! //add the return
}
} else {
if (indexPath.section == 0) {
if let cell = tableView.dequeueReusableCellWithIdentifier("FriendRequestCell", forIndexPath: indexPath) as? FriendRequestCell {
let friendRequestCell = friendRequests[indexPath.row]
cell.configureCell(friendRequestCell)
return cell! //add the return
}
}
}
}
return FriendCell()
}
答案 1 :(得分:1)
好的,这是多种类型的单元格的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if searching == true {
let cell = tableView.dequeueReusableCellWithIdentifier("AddFriendCell", forIndexPath: indexPath) as! AddFriendCell
// ...configure your cell...
return cell
}
else{
if friendRequests.isEmpty || (indexPath.section == 1) {
let cell = tableView.dequeueReusableCellWithIdentifier("FriendCell", forIndexPath: indexPath) as! FriendCell
// ...configure your cell...
return cell
}
else{
if (indexPath.section == 0) {
let cell = tableView.dequeueReusableCellWithIdentifier("FriendRequestCell", forIndexPath: indexPath) as! FriendRequestCell
// ...configure your cell...
return cell
}
else {
// Decide what to do if section is NOT 0. If this CAN happen
// and you don't have a cell type to return, it is a design flaw.
// don't add a adummy "return FriendCell()" as a fallback for a
// case that should never happen, to make the compiler happy.
// This type of error should be caught during development.
}
}
}
}
(参见关于如何处理未经移植的执行路径的评论段落)
或者,您可以在所有if / else块之外将cell
声明为var
类型UITableViewCell
,将其分配给内部适当的已出列单元格,(即删除{ {1}}关键字,如果修改上面的代码,并在最后返回。
但是你仍然需要确保在返回之前初始化它。