我通常使用MVC,HTML和JavaScript。因此,我习惯于使用HTML表。我已经用Swift编写了一个用xcode编写的iOS应用,它通过一个自定义单元格显示了表格(以下代码):
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
//Load cell
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! StatModelViewCell
//Map data
let data = TableData.ListOfStatsModels[indexPath.row]
if(Landscape == true)
{
//Map data to elements
cell.LabelMemberName.text = data.MemberName
cell.LabelGP.text = "GP:" + String(data.GP)
cell.LabelW.text = "W:" + String(data.W)
cell.LabelD.text = "D:" + String(data.D)
cell.LabelL.text = "L:" + String(data.L)
cell.LabelGoalsScorePositive.hidden = false
cell.LabelGoalsScorePositive.text = "+" + String(data.GoalsScorePositive)
cell.LabelGoalsScoreNegative.hidden = false
cell.LabelGoalsScoreNegative.text = "-" + String(data.GoalsScoreNegative)
cell.LabelP.hidden = false
cell.LabelP.text = "P:" + String(data.P)
cell.LabelP_GP.hidden = false
cell.LabelP_GP.text = "P/GP:" + String(data.P_GP)
}
else
{
//Map data to elements
cell.LabelMemberName.text = data.MemberName
cell.LabelGP.text = "GP:" + String(data.GP)
cell.LabelW.text = "W:" + String(data.W)
cell.LabelD.text = "D:" + String(data.D)
cell.LabelL.text = "L:" + String(data.L) + " P/GP:" + String(data.P_GP)
cell.LabelGoalsScorePositive.hidden = true
cell.LabelGoalsScoreNegative.hidden = true
cell.LabelP.hidden = true
cell.LabelP_GP.hidden = true
}
return cell
}
在视图中看起来像这样:
我真的很想使该应用程序像普通的HTML表一样,因此看起来像这样:
但是我真的不知道如何在iOS应用中制作表格。
我曾在堆栈溢出方面查看过有关此主题的其他文章,但是它们通常是用目标c编写的,或者您必须为此使用自定义框架,并且它们通常反应不太灵敏。
任何人都可以通过swift提供一个简单的表格示例(其中包含标头和从数组读取的数据)来帮助我吗?
最佳答案
如果要显示图像中显示的布局,可以尝试使用Collection视图而不是tableview,可以尝试此github演示
https://github.com/brightec/CustomCollectionViewLayout,我在项目中使用了它,效果很好,看起来像您需要的
它使用UICollectionViewLayout
创建布局并显示基于数组的
如果需要图像中显示的边框,则可以像ContentCollectionViewCell
这样将边框赋予contentCell.layer.borderWidth
图层,以及border的其他属性