在cellForRowAtIndexPath中动态实现Section

时间:2015-12-01 05:31:12

标签: ios swift uitableview

我无法围绕cellForRowAtIndexPath中的部分实现。

我有UITableView,其中我想展示2个部分。

  • 传入的朋友请求
  • 朋友

在Storyboard中,我将UITableView Style更改为Grouped

接下来,如果没有朋友请求,我希望没有Friend Request部分。在viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()
    (...)
    if friendRequests.isEmpty {
        friendsDataSource = friends
    } else {
        friendsDataSource = [friendRequests, friends]
    }
}

其余的:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return friendsDataSource.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return friendsDataSource[section].count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {        
    let friendRequest = friendsDataSource[0][indexPath.row]
    let friend = friendsDataSource[1][indexPath.row]

    if let cell = tableView.dequeueReusableCellWithIdentifier("FriendCell") as? FriendCell {
        cell.configureProfileCell(userProfile)
        return cell

    } else {
        return FriendCell()
    }
}

我知道我的cellForRowAtIndexPath很恶心,但我完全不知道如何实施它。

任何正确方向的帮助,非常感谢

2 个答案:

答案 0 :(得分:2)

发现了if (indexPath.section == 0),我刚刚破解了它。

我的眼睛看着这样疼,所以请发布更好的方法。现在:

var friendRequests = [FriendRequest]()
var friends = [UserProfile]()

var friendsDataSource = []

override func viewDidLoad() {
    super.viewDidLoad()

    friends = FriendManager.instance.myFriends
    friendRequests = FriendManager.instance.incomingFriendRequests

    if friendRequests.isEmpty {
        friendsDataSource = [friends]
    } else {
        friendsDataSource = [friendRequests, friends]
    }
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return friendsDataSource.count
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return friendsDataSource[section].count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if let cell = tableView.dequeueReusableCellWithIdentifier("FriendCell", forIndexPath: indexPath) as? FriendCell {

        if friendRequests.isEmpty {
            let friendCell = friends[indexPath.row]
            cell.configureProfileCell(friendCell)
        } else {
            if (indexPath.section == 0) {
                let friendRequestCell = friendRequests[indexPath.row]
                cell.configureRequestCell(friendRequestCell)
            } else if (indexPath.section == 1) {
                let friendCell = friends[indexPath.row]
                cell.configureProfileCell(friendCell)
            }
        }
        return cell
    } else {
        return FriendCell()
    }
}

答案 1 :(得分:0)

您应该使用其他更新的出列方法:dequeReusableCellWithIdentifier(_:forIndexPath:)代替(传递实际的索引路径)。

确保一个人永远成功,所以你可以不用这个if/else结构:

if let cell = ... {
    ...

    return cell
}
else {
    return FriendCell()
}

顺便说一句,您正在返回FriendCell实例,而不进行配置。那是你真正想要的吗?

<强>澄清 只有当一个或多个具有指定标识符的单元已经入队以供重用时,方法dequeReusableCellWithIdentifier(:)才会成功;你调用它的前几次它将返回nil和你需要回退来实例化一个新的单元格(具有相同的标识符),以便立即使用(以后再使用):

func tableView(tableView:UITableView, cellForRowAtIndexPath:NSIndexPath) -> UITableViewCell
{
    if let cell = tableView.dequeReusableCellWithIdentifier("Identifier") as? FriendCell {
        // Successfully dequeued for reuse;
        // configure it:

        // (set labels' texts, etc.)

        return cell
    }
    else{
         // No cell enqueued; create anew

         let cell = FriendCell(style:.Plain, reuseIdentifier:"Identifier")

         // configure it

         // (set labels' texts, etc.)

         return cell
    }
}  

...但是因为这项检查很痛苦,Apple又添加了一种新方法:

dequeReusableCellWithIdentifier(identifier:String, forIndexPath:NSIndexPath)

在内部执行出队,也会在没有人可用时初始化新单元。这消除了上面代码中对else路径的需求,并且它变得更加智能:

func tableView(tableView:UITableView, cellForRowAtIndexPath:NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeReusableCellWithIdentifier("Identifier", forIndexPath:indexPath) as! FriendCell
    // (Never fails - provided identifier is right and class is registered for it)

    // configure it:

    // (set labels' texts, etc.)

    return cell
}
相关问题