Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 我们是否可以在swift中的UITableView节中实现节?_Ios_Swift_Uitableview - Fatal编程技术网

Ios 我们是否可以在swift中的UITableView节中实现节?

Ios 我们是否可以在swift中的UITableView节中实现节?,ios,swift,uitableview,Ios,Swift,Uitableview,假设有一个UITableView,包含两个部分a和B。现在,在这两个部分中,将显示多个带有详细信息的对象,例如,如果对象是汽车,则有多个汽车,其名称、颜色、里程将显示在对象中。那么,如果我们必须使用UITableView来实现它,那么如何以最佳方式解决这个问题呢 我为UITableView提供了两个部分。然后,我将对象显示为部分中的行。然后,如果我只显示一个对象的3个细节,那么我将提供行数为3*(对象数)。以下代码写入UITableViewCell类中,并从持有UITableView对象的数据源

假设有一个
UITableView
,包含两个部分a和B。现在,在这两个部分中,将显示多个带有详细信息的对象,例如,如果对象是汽车,则有多个汽车,其名称、颜色、里程将显示在对象中。那么,如果我们必须使用
UITableView
来实现它,那么如何以最佳方式解决这个问题呢

我为
UITableView
提供了两个部分。然后,我将对象显示为部分中的行。然后,如果我只显示一个对象的3个细节,那么我将提供行数为3*(对象数)。以下代码写入
UITableViewCell
类中,并从持有
UITableView
对象的数据源方法cellForRowAt()的
UIViewController
调用

func配置SectionAcellWith(indexPath:indexPath,数据:[Car]){
var row=indexPath.row
行=行%3
开关行{
案例0:carNameLabel.text=data[indexath.row/3].name
案例1:colorLabel.text=data[indexath.row/3].color
案例2:mileageLabel.text=data[indexath.row/3]。里程
违约:
打破
}
}
同样,为了配置B部分的单元,重复上述代码

但是正如您可能看到的,很多东西都是硬编码的,比如row=row%3,对于一个新开发人员来说,理解代码是很困难的。还有代码的重复(配置部分A和B的单元格)。
是否有其他方法当然是解决这种情况的最佳方法?

如果每个部分的布局/细节可能相同,您可以只创建一种类型的单元,并在两个部分中使用它:

class DetailCell: UITableViewCell {
  static let cellID = "DetailCell"
 //set up all the images/labels etc

  func configure(with car: Car) {
    label1.text = car.name
    label2.text = car.color
    label3.text = car.mileage
  }
  func configure(with other: SomeOtherType) {. //whatever Section B displays
    label1.text = other.property1
    label2.text = other.property2
    label3.text = other.property3
  }
}
您不需要担心单元格定义的索引路径或行。这是在dataSource和delegate函数中完成的

您将在
UITableViewDataSource
numberOfRows
方法中设置每个部分的行数,并在
cellForRowAt
方法中创建所有单元格。我不会在这里重复所有细节-在TableView上有100多个教程和博客-但会给您提供摘要示例,让您知道您的目标:

override func viewDidLoad(){
super.viewDidLoad()
tableView.register(DetailCell.self,forCellReuseIdentifier:DetailCell.cellID)
//任何其他设置
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
开关部分:
案例0:return carsArray.count//如果您在A区展示车辆
案例1:返回someOtherArray.count//无论您在第B节中显示什么
默认值:返回0
}
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(标识符:DetailCell.cellID,for:indexath)
如果indexath.section==0{
configure(使用:carsArray[indexPath.row])
}如果节==1,则为else{
configure(使用:someOtherArray[indexPath.row])
}
//还有什么牢房吗
返回单元
}

如果每个部分的布局/详细信息可能相同,则只需创建一种类型的单元并在两个部分中使用:

class DetailCell: UITableViewCell {
  static let cellID = "DetailCell"
 //set up all the images/labels etc

  func configure(with car: Car) {
    label1.text = car.name
    label2.text = car.color
    label3.text = car.mileage
  }
  func configure(with other: SomeOtherType) {. //whatever Section B displays
    label1.text = other.property1
    label2.text = other.property2
    label3.text = other.property3
  }
}
您不需要担心单元格定义的索引路径或行。这是在dataSource和delegate函数中完成的

您将在
UITableViewDataSource
numberOfRows
方法中设置每个部分的行数,并在
cellForRowAt
方法中创建所有单元格。我不会在这里重复所有细节-在TableView上有100多个教程和博客-但会给您提供摘要示例,让您知道您的目标:

override func viewDidLoad(){
super.viewDidLoad()
tableView.register(DetailCell.self,forCellReuseIdentifier:DetailCell.cellID)
//任何其他设置
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
开关部分:
案例0:return carsArray.count//如果您在A区展示车辆
案例1:返回someOtherArray.count//无论您在第B节中显示什么
默认值:返回0
}
}
重写func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
let cell=tableView.dequeueReusableCell(标识符:DetailCell.cellID,for:indexath)
如果indexath.section==0{
configure(使用:carsArray[indexPath.row])
}如果节==1,则为else{
configure(使用:someOtherArray[indexPath.row])
}
//还有什么牢房吗
返回单元
}

我看不出有任何理由在节中实现节。您应该使用定制的
IUTableViewCell
,其布局可以显示车辆的所有细节。你可以在单元设计中构建“部分”,如果你想要这样的外观的话。通过这种方式,您可以利用
UITableView
提供的“开箱即用”功能,请参见下面的示例。否则,您将面临如何设计
UITableView
的问题。我看不出有任何理由在小节中实现小节。您应该使用定制的
IUTableViewCell
,其布局可以显示车辆的所有细节。你可以在单元设计中构建“部分”,如果你想要这样的外观的话。通过这种方式,您可以利用
UITableView
提供的“开箱即用”功能,请参见下面的示例。否则,您将与
UITableView
的设计方式背道而驰。