ios - 如何在 UITableView 中使用多个自定义单元格 (>1)

标签 ios swift uitableview custom-cell

我在 Stack Overflow 上搜索了这个问题的答案,并找到了一些有用的答案,但我的情况有所不同,因为该部分中的行数是根据数组中列出的项目数确定的。我正在尝试创建一个使用两个自定义单元格的表格。第一个单元格显示个人资料信息,第二个单元格显示新闻源。

 func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return myProfileDM.profileArray.count
    //return myProfileFeedDM.profileFeedArray.count
}


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
    UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCellWithIdentifier("bio", forIndexPath:indexPath) as! ProfileTableViewCell

            cell.followerNumber!.text = myProfileDM.profileArray[indexPath.row].followerNumberInterface
            cell.followers!.text = myProfileDM.profileArray[indexPath.row].followersInterface
            cell.following!.text = myProfileDM.profileArray[indexPath.row].followingInterface
            cell.followingNumber!.text = myProfileDM.profileArray[indexPath.row].followingNumberInterface

        return cell
        }
        else{
            let cell = tableView.dequeueReusableCellWithIdentifier("feed", forIndexPath:indexPath) as! FeedTableViewCell

            //let cell: FeedTableViewCell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "feed")
            cell.profileFeedLabel!.text = myProfileFeedDM.profileFeedArray[indexPath.row].profileFeed
            cell.profileDateLabel!.text = myProfileFeedDM.profileFeedArray[indexPath.row].profileDate

        return cell
        }


}


}

当我运行程序时,第一个单元格(带有标识符-bio)是唯一加载/显示的单元格。

最佳答案

我认为该部分中的行数是错误的。从你的变量名称来看,我怀疑它应该是

myProfileFeedDM.profileFeedArray.count + 1

请注意,在 Feed 数组中,您必须使用 indexPath.row - 1 才能获取数组的正确索引,因为第一行用于配置文件。

关于ios - 如何在 UITableView 中使用多个自定义单元格 (>1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32284464/

相关文章:

ios - Xcode Swift : is being asked to animate its opacity. 这将导致效果出现损坏,直到不透明度返回到 1

ios - 从 MyTableViewController 中的方法 'viewDidLoad' 获取 MyTableViewCell?

iphone - UITableView 核心数据重新排序

iphone - UITableView/UITableViewCell 点击事件响应?

ios - 在 Ubuntu 上发布未签名的基于 Cordova 的 iOS 应用程序

ios - CSS 渐变不适用于 iOS

ios - 为什么 UITableViewAutomaticDimension 不起作用?

ios - 如何将大字符串传递给 iMessage 扩展?

javascript - IOS设备隐藏 "Play Store"图片,Android设备隐藏 "App Store"图片

ios - 如何在uibutton之类的消息应用程序上使用角标(Badge)?