Unter der Annahme, dass Sie wissen, wie Sie Ihre individuelle Zelle erstellen (wenn Sie nicht this question prüfe) und die erforderliche Datenquelle Methoden implementieren, sollten Sie dies tun in cellForRowAt
oder cellForItem
Methoden cellForRowAt
im Code -Ich mit snippet-:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// first row should display a banner:
if indexPath.row == 0 {
let bannerCell = tableView.dequeueReusableCell(withIdentifier: "BannerTableViewCell") as! BannerTableViewCell
// ...
return bannerCell
}
// second row should display categories
if indexPath.row == 1 {
let categoriesCell = tableView.dequeueReusableCell(withIdentifier: "CategoriesTableViewCell") as! CategoriesTableViewCell
// ...
return categoriesCell
}
// the other cells should contains title and subtitle:
let defaultCell = tableView.dequeueReusableCell(withIdentifier: "CategoriesTableViewCell") as! TileAndSubtitleTableViewCell
// ...
return defaultCell
}
Machen Sie es besser lesbar:
Sie auch enum
zur Überprüfung der indexPath.row
statt vergleichen sie mit ints definieren:
enum MyRows: Int {
case banner = 0
case categories
}
Jetzt können Sie mit lesbaren Werten vergleichen:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// first row should display a banner:
if indexPath.row == MyRows.banner.rawValue {
let bannerCell = tableView.dequeueReusableCell(withIdentifier: "BannerTableViewCell") as! BannerTableViewCell
// ...
return bannerCell
}
// second row should display categories
if indexPath.row == MyRows.categories.rawValue {
let categoriesCell = tableView.dequeueReusableCell(withIdentifier: "CategoriesTableViewCell") as! CategoriesTableViewCell
// ...
return categoriesCell
}
// the other cells should contains title and subtitle:
let defaultCell = tableView.dequeueReusableCell(withIdentifier: "CategoriesTableViewCell") as! TileAndSubtitleTableViewCell
// ...
return defaultCell
}
tks dich so sehr, ich werde es jetzt versuchen –