How to Access to a Static Cell

How to access to a static cell

For static cells created in the storyboard, you can simply set the IBOutlet for the elements you want to edit by ctrl-dragging from the storyboard to the corresponding view controller, to end up with something like this:

class MyViewController: UIViewController {
@IBOutlet weak var cell1: UITableViewCell!
@IBOutlet weak var cell2: UITableViewCell!
}

Then you can access the built-in elements in the Basic view with cell1.textLabel etc.

Check out the documentation about setting IBOutlets.

To change the background color, you can do it in the storyboard UI directly or access the backgroundColor property. You might want to read the UITableViewCell Class Reference.

How to access to a textLabel in a static cell

What I have done after a lot of testing and reading. I have create a segue to the statutable class.

if (segue.identifier == "myEmbeddedSegue") {
let childViewController = segue.destination as! hondDetialTableViewController
childViewController.hondId = hondData["hondId"]!
}

In this segue I send only the hondId, everything else i ask entities.

UITableViewController accessing static cells programmatically issue

To access statically created cells, try this:

UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

This works for static cells. So, if you're in the...

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];

return cell;

}

... delegate, you can access all statically configured cells using the above declaration. From there, you can do what ever you want with "cell".

I had a ViewController that had two UITableViews on it. One of them had cells defined statically, with a Storyboard, and the other had cells defined dynamically using code. Given I was using the same ViewController as delegate for both tables, I needed to prevent new cells from being created where cellForRowAtIndexPath was being called where cells had already been created.

In your case, you need to gain programmatic access to your cells.

Have fun.

Access to a detailTextLabel of a static cell in the function cellForRowAt

Don't use data source methods like cellForRowAt and reloadData() if the cells are static.

Use IBOutlets instead and assign the properties directly.

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
}

How to properly implement a static cell with Swift 3

It's much easier if the table view contains only static cells:

  • In Interface Builder select the table view and set the Content to Static Cells
  • Create IBOutlets and IBActions in the controller class and connect them.
  • Implementing table view data source methods and subclassing cells is not needed.

Access static cell when pressing button

You are missing a closing parenthesis on the method call. Add a second ) before your as keyword. You will also need to use as!.

You need to change:

let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 2, inSection: 1) as myStaticCell

To this:

let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 2, inSection: 1)) as! myStaticCell

Fire a method from a Static Cell in a Table view Controller

In the viewController add:

@property (nonatomic, weak) IBOutlet UITableViewCell *theStaticCell;  

Connect that outlet to the cell in the storyboard.

Now in tableView:didSelectRowAtIndexPath method:

UITableViewCell *theCellClicked = [self.tableView cellForRowAtIndexPath:indexPath];
if (theCellClicked == theStaticCell) {
//Do stuff
}

Connect static cell with an action

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
if (indexPath.section ==1 && indexPath.row == 0)
{
//Do what you want to do.
}
}

OR

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
// Cell will be deselected by following line.
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UITableViewCell *staticCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];

if (cell == staticCell)
{
//Do what you want to do.
}
}


Related Topics



Leave a reply



Submit