0

I have added a UITableView inside a UIViewController in IB, I have set the TableView content to "Static Cells" since that's what I want, and everything seems fine when I haven't added any content to the UITableView. But if I for example change the first UITableViewCell's background color to black it doesn't display black when running the app. I have added UITableViewControllerDelegate and UITableViewDataSource and set the tableView.delage = self;. But still no changes I make to the tableView displays when I run the app.

What can the problem be?

NOTE: I have also tried to add tableView.dataSource = self;, but that just make the app crash.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Peter
  • 1,848
  • 4
  • 26
  • 44

5 Answers5

0

Yes, you can have static table view content in UIViewController.

All you need to do is:

-Create the table's static cells in interface builder and design them the way you like.

-Make the UIViewController implement table view's data source and delegate:

@interface MyViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>

-Connect the table view's delegate and dataSource to the view controller in interface builder

-Implement -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section to return the number of your cells. (e.g. return 10, yes simple as that)

-Connect your cells to your code as IBOutlets in Interface Builder. IMPORTANT: Make sure they are strong, weak won't work. e.g. @property (strong, nonatomic) IBOutlet UITableViewCell *myFirstCell;

-Implement -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath to return the correct cell at index path. e.g:

int num = indexPath.row;
UITableViewCell *cell;
switch (num) {
    case 0:
        cell = self.myFirstCell;
        break;
    case 1:
        cell = self.mySecondCell;
        break;
}
return cell;

If you apply all these steps, you should have working static cells that works for tables with not many cells. Perfect for tables that you have a few (probably no more than 10-20 would be enough) content. I've ran the same issue a few days ago and I confirm that it works. More info check here: Best approach to add Static-TableView-Cells to a UIViewcontroller?

Community
  • 1
  • 1
Kakshil Shah
  • 3,466
  • 1
  • 17
  • 31
  • Are you sure I can use static content in a UIViewController? @Gavin says you are only supposed to use static content within UITableViewController. – Peter Jan 08 '14 at 21:03
  • I suggest you copy paste the code and test.. the code above worked for me, so has to be right. – Kakshil Shah Jan 09 '14 at 10:47
0

You will want to use a UITableViewController, not a UIViewController with a UITableView added to it, because you're only supposed to use static cells with a UITableViewController. There are probably ways to hack around it so you can get the static cells to work, but it's much simpler to just use a UITableViewController, and you'll have fewer issues to deal with, especially if you ever change the content of the table.

Gavin
  • 8,204
  • 3
  • 32
  • 42
0

Seems you have problem with the background issue for UITableViewCell. So don't use background for checking if content is drawing or not.

You can use debugger for this for example or NSLog.

NOTE: the cell has content view that can be modified. I don't remember but seems the cell has not got background property that can be adjusted with a color.

If you tried this line below e.g. - it will no work and color will be white as default color.

[cell setBackgroundColor:[UIColor blackColor]];

Try to add something to the cell for example picture and then you can see the result as I think.

Use this code:

[cell.contentView setBackgroundColor:[UIColor blackColor]]; in this delegate

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

it will help you as I think.

Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
0

Have you implementede the protocol? ... another thing is that when implementing the protocol i had an issue when no cell was displayed.. try with this implementation for the given method.

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

    NSString *CellIdentifier=@"Cell";
    CobranzaCell *cell = [[CobranzaCell alloc]init];
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                                           forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[CobranzaCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    // Configure the cell...
    return cell;        
}
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
0

You cannot use the static cells in simple UIViewController subclass. Instead, you should use the instance of UITableViewController. The hack is in your storyboard drag the instance of UIViewController to your storyboard canvas. Then, drag the Container View from objects library and drop it to your UIViewController's view. By default it will create the embed segue with related UIViewController instance. What you want to do - delete this view controller, drag and drop instance of UITableViewController from objects library, then right click and drag from your Container View to just dropped UITableViewController. Chose embed. Now your table view controller gets the size of container view and you can use static cells in it! Hope it will help!