本文介绍了在 cellForRowAtIndexPath 中动态实现部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解 cellForRowAtIndexPath 中部分的实现.

I am not able to wrap my head around the implementation of sections in cellForRowAtIndexPath.

我有一个 UITableView,我想在其中显示 2 个部分.

I have a UITableView in which I would like to show 2 sections.

  • 传入的好友请求
  • 朋友

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

In Storyboard, I change my UITableView Style to Grouped.

接下来,如果没有好友请求,我希望没有 Friend Request 部分.在 viewDidLoad 中:

Next, I would like there to be no Friend Request section if there are no friend requests. In 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 很恶心,但我完全不知道如何实现它.

I know my cellForRowAtIndexPath is disgusting but I have absolutely no idea how to implement it.

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

Any help in the right direction, greatly appreciated

推荐答案

发现 if (indexPath.section == 0),我只是解决了这个问题.

Discovered if (indexPath.section == 0), and I just hacked around that.

看到这个我眼睛疼,所以请发布更好的方法.目前:

My eyes hurt looking at this so Please post better ways of doing this. For now:

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()
    }
}

这篇关于在 cellForRowAtIndexPath 中动态实现部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 00:19