Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 不同的UITableViewCells重叠_Ios_Swift_Uitableview - Fatal编程技术网

Ios 不同的UITableViewCells重叠

Ios 不同的UITableViewCells重叠,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个带有3个原型单元和3个自定义单元类的UITableView: FriendCell FriendRequestCell和AddFriendCell 初始化后,表格显示朋友 如果有任何好友请求,它会在上面的好友部分显示它们 如果没有好友请求,则只显示好友 但是,我还有一个UISearchBar,用于搜索用户,当它有结果时,应该返回AddFriendCells并重新加载表 相反,我得到的是: 代码: 关于问题的根源有什么想法吗?如果方法findObjectsInBackgroundWit

我有一个带有3个原型单元和3个自定义单元类的
UITableView
FriendCell
FriendRequestCell
AddFriendCell

初始化后,表格显示
朋友

如果有任何
好友请求
,它会在上面的
好友
部分显示它们

如果没有
好友请求
,则只显示
好友

但是,我还有一个
UISearchBar
,用于搜索用户,当它有结果时,应该返回
AddFriendCell
s并重新加载表

相反,我得到的是:

代码:


关于问题的根源有什么想法吗?

如果方法
findObjectsInBackgroundWithBlock
是异步的,我认为您可以使用
self.tableView.reloadData()
而不是
dispatch\u async(dispatch\u get\u main\u 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()
}

好的,下面是多种类型单元格的代码:

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.
            }
        }
    }
}
(请参阅有关如何处理未移植的执行路径的评论段落)

或者,您可以将
单元格
声明为
UITableViewCell
类型的
var
在所有if/else块之外,将其分配给内部适当的出列单元格(即,如果修改上述代码,则删除
let
关键字),并在最后返回它


但您仍然需要确保它在返回之前已初始化。

这可能与您尝试退出队列的单元格大小有关。由于您要将不同类型的单元格出列,因此您的
func tableView(\utableview:UITableView,heightForRowAtIndexPath:nsindepath)->CGFloat
实现应该根据您使用的单元格类型返回不同的大小。@champul我在代码中没有涉及单元格大小。是否需要解决这个问题?您的单元格是否有不同的高度?在我看来,您的代码似乎有点过于复杂。为什么一个用户界面几乎相同的单元格有3种不同的单元格。同样的事情也可以通过一个单元格类来实现,它的UI是根据它的类型来布局的。@Champoul,因为UI会随着时间的推移而发展。不,他们没有不同的高度!这就是我最初拥有的,我一直都是这样做的。。。刚换回来。有关于重叠的想法吗?你不回你cell@Rose:不应返回空的(未配置的单元格);请看我对他上一个问题的回答:@NicolasMiari你能展示一下你将如何实施你对这种情况的建议吗?我一直试图在条件的范围之外声明一个单元格,以返回配置的单元格,但是这并不起作用。正如我所说,只要
标识符
与表中注册的
UITableViewCell
子类相匹配(以编程方式或在IB中),则出列方法永远不会失败。只需根据具体情况将适当类型的单元格出列并强制转换、配置并返回即可。明白了。最多只有2个部分,因此通过删除
如果(indexPath.section==0)
检查,
返回单元格
将变得详尽,并且不需要返回dummy
FriendCell
。。非常感谢你的帮助。
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.
            }
        }
    }
}