我在 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/