Dynamic Tableview inside a Static tableview Cell
Well after astonishing efforts regarding this one, I've found the solution.
Concerning the following:
Swift: TableView within Static UITableViewCell
Where the problem solver says : As far as I can determine by experimenting with this, you can't use the same UITableViewController as the data source and delegate of both table views. With a static table view, you're not supposed to implement the data source methods at all. The strange thing is, even if I disconnect the data source and delegate connections between my static table view and the table view controller, that table view still calls numberOfRowsInSection in my table view controller class. If I explicitly set the data source to nil in code, that stops it from calling the data source methods, but the embedded dynamic table view also fails to call them, so this structure doesn't work.
However, you can get around this by using a different object to be the data source and delegate of your embedded dynamic table view. Make an IBOutlet for your embedded table view, and set its data source and delegate to this new object (The class is DataSource in this example, and it's a subclass of NSObject).
I've modified my code in this way now :
import Foundation
import UIKit
import MessageUI
class DataSource: NSObject, UITableViewDataSource, UITableViewDelegate {
var items : [String] = ["GRE Test Structure ","GRE Score "]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellNew", forIndexPath: indexPath) as! MainTableViewCell
cell.customCell01.text = items[indexPath.row]
return cell
}
}
class MenuController: UITableViewController,MFMailComposeViewControllerDelegate {
@IBOutlet var tablle0: UITableView!
@IBOutlet weak var tablle: UITableView!
var dataSource = DataSource()
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem =
self.editButtonItem()
tablle.delegate = dataSource
tablle.dataSource = dataSource
}
}
Now it works exactly fine.
UITableView Mix of Static and Dynamic Cells?
As you stated you can't mix static and dynamic cells. However, what you can do is break up the content into different data arrays that correspond to each group. Then break the table up into difference sections and load the data from the correct array in cellForRowAtIndexPath:.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"CELLID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
switch (indexPath.section) {
case 0:{
cell.textLabel.text = self.arrayOfStaticThings1[indexPath.row];
}break;
case 1:{
cell.textLabel.text = self.arrayOfDynamicThings[indexPath.row];
}break;
case 2:{
cell.textLabel.text = self.arrayOfStaticThings2[indexPath.row];
}break;
default:
break;
}
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section) {
case 0:{
return self.arrayOfStaticThings1.count;
}break;
case 1:{
return self.arrayOfDynamicThings.count;
}break;
case 2:{
return self.arrayOfStaticThings2.count;
}break;
default:
return 0;
break;
}
}
Swift - Create a UITableViewController with one static cell at the top and the rest be dynamic cells?
Don't use Static Cells
. Choose Dynamic Prototypes
in your table view and create 2 prototype cells.
And return first cell in first section, other cells in second section
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
return 1
} else if section == 1 {
return sortedSongs.count
} else if section == 2 {
return anotherArrayCount
}
return 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "FirstCell", for: indexPath) as! FirstCell
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "RecentCell", for: indexPath) as! RecentCell
//cell.songTitle.text = albumList[indexPath.row]
//cell.songArtist.text = artistList[indexPath.row]
//cell.songImage.image = imageList[indexPath.row]
return cell
}
}
Implement a dynamic table within a static table view (cell)
Okay thank you rMickeyD for your tip. I used it partially and implemented it. You can see the result on this screenshot here:
Adding to this I set the height of the cell in the static table with a prepareToSegue to the array of the guests.count * 44 for the cell height in the right.
Code for the static table view (left):
import UIKit
class EventDetailTableViewController: UITableViewController {
var attendeesArrayFromDatabase = ["Kathi", "Cansin", "Tyrone", "Manuel", "Stavros", "Christoph", "Maria", "Aditya", "Kathi", "Cansin", "Tyrone", "Manuel", "Stavros", "Christoph", "Maria", "Aditya"]
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 1 && indexPath.row == 0 {
let height:CGFloat = CGFloat(attendeesArrayFromDatabase.count * 44)
return height
}
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}
// MARK: Navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let destViewController : EventPeopleJoinedContainerTableViewController = segue.destinationViewController as! EventPeopleJoinedContainerTableViewController
destViewController.attendeesArray = attendeesArrayFromDatabase
}
}
and the right tableview for the dynamic table:
import UIKit
class EventPeopleJoinedContainerTableViewController: UITableViewController {
var attendeesArray = []
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return attendeesArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("personJoined", forIndexPath: indexPath) as UITableViewCell
//Create a label with the name of a person from array attendeesArray
let attendeeNameLabel = UILabel(frame: CGRect(x: 70, y: 0, width: cell.frame.width, height: cell.frame.height))
attendeeNameLabel.font = cell.textLabel?.font.fontWithSize(14.0)
attendeeNameLabel.textColor = UIColor.darkGrayColor()
attendeeNameLabel.text = attendeesArray[indexPath.row] as? String
//Create a image view for the profile picture of the guest
let attendeeImageView = UIImageView(frame: CGRect(x: 25, y: 7, width: 30, height: 30))
attendeeImageView.layer.cornerRadius = attendeeImageView.frame.size.width/2
attendeeImageView.layer.borderColor = UIColor.whiteColor().CGColor
attendeeImageView.layer.borderWidth = 0.5
attendeeImageView.clipsToBounds = true
attendeeImageView.image = UIImage(named: "profilPicDummy")
cell.contentView.addSubview(attendeeNameLabel)
cell.contentView.addSubview(attendeeImageView)
return cell
}
}
Static UITableView, make a single cell with dynamic height in Swift
Have you tried this?
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (indexPath.row < 11) {
// Everything starts at 0, so this covers 0-10
// Whatever your cell height is
return <#fixedCellHeight#>
} else {
// This is your 12th cell (indexPath.row 11), as we start counting at 0
return UITableViewAutomaticDimension
}
}
Swift: How to use static cell in dynamic UITableView
Have you tried something like this?
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
if (indexPath.row == 1) {
cell.textLabel?.text = "test"
} else {
// Configure the cell...
}
return cell
}
Related Topics
How to Debug an Issue with a Release Mode Build in iOS
iOS 11+ How to Migrate Existing Core Data to Shared App Group for Use in Extension
Scanning Barcode or Qr Code in Swift 3.0 Using Avfoundation
Customize Navigation Bar with Title View
Exception with Insertobject:Atindex: on iOS6
How to Access iOS Private APIs in Swift
View Controller Responds to App Delegate Notifications in iOS 12 But Not in iOS 13
How to Use Mbprogresshud with Swift
How to Receive Push Notifications of an Other App
iOS 10 Rich Media Push Notification (Media Attachment) in Objective-C
Is There Any Limitation to Distribution of Apps in iOS Enterprise Program
Move Xcode Project Causes Red Text (Missing Files from Project)
Add a Done Button Within a Pop-Up Datepickerview in Swift