1

I am trying to embed a UITableView in my View Controller (not a TableViewController). I am unable to get any of my data to show. Here is the relevant code:

M file:

@interface ViewController () <CBCentralManagerDelegate, CBPeripheralDelegate, UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray        *Device_array_name;

@end

. . .

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }


   cell.textLabel.text=self.Device_array_name[indexPath.row];


    return cell;
}

I've also defined number of sections and rows, and my array does indeed have content. I'm pretty new to iOS development so I've no doubt missed something.

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 2
    Is your `numberOfRowsInSection` method being called? Does it return a non-zero value? Is `tableView` non-nil? Is its `delegate` and `dataSource` set properly? Is the table view's frame set properly? – rmaddy Dec 21 '15 at 05:06
  • where you allocating and adding objects in `Device_array_name` array ? please try to reload table after adding data in array. – Will Smith Dec 21 '15 at 06:18

7 Answers7

6

Delegates & Datasources should be defined for UI Controls to get called their respective delegates.

For Instance : In your viewDidLoad function define

tableView.delegate = self
tableView.datasource = self

It will work.

Kumar KL
  • 15,315
  • 9
  • 38
  • 60
Zumry Mohamed
  • 9,318
  • 5
  • 46
  • 51
2

Set the array allocation in your view did load.

Device_array_name=[NSMutableArray alloc]init];

Set the tableview delegates in viewdidLoad

tableview.delegate=self;
tableview.datasource=self;

After give the count of an array in Number of rows in section

return Device_array_name.count;

In your cellForRow AtIndexpath method

  cell.textLabel.text=[self.Device_array_name objectAtIndex:indexPath.row];
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
sasikala
  • 21
  • 2
1

There could be 4 reason (most probably)

1> You delegate and datasource are not set

tableView.delegate = self
tableView.datasource = self

2> Your datasource array is nil (Device_array_name)

3> Your IBOutlet is not connected with tableview

4> Your tableview frame is becoming zero due to autloayout or something else (check console at runtime)

Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
0

I think you just need to initialize the delegate and dataSource for your tableView property to your ViewController.

LavaSlider
  • 2,494
  • 18
  • 29
0

If you embed this way, you don't have default setting for the UITableView like you have when you use UITableViewController directly. You will need to set it up by yourself. Most likely, it's because you have not wired up your delegation object. Second will be, your UITableView is invisible. That also causes the problem because the delegate methods will not be called.

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
0

i think go to storyborad and set delegate,datasource in there.if you using by storyborad.i hope will work.

https://i.stack.imgur.com/HYDDA.png

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
0

Here is my list of the possible problems:

  1. The @IBOutlet for the table view is not connected in Interface Builder. Solution: check connection in Interface Builder.
  2. The outlet is set, but the view controller placed in the storyboard is not of your custom UIViewController subclass, but the default UIViewController. In this case, none of your code will be executed. Solution: specify the view controller's class is the identity inspector (center tab).
  3. The dataSource property of the table view is not set to your view controller. Solution: Assign it in either Interface Builder (storyboard) or programmatically inside viewDidLoad() or later.
  4. You are not implementing the UITableviewDataSource protocol method: tableView(_:numberOfRowsInSection:) (i.e., the table view "thinks" it has 0 rows to display). In this case, tableView(_:cellForRowAtIndexPath:) will not be called (you can confirm this by placing a break point). Solution: Implement the method and return the count property of your model object array.
  5. The array that serves as the data source for your table view is empty (has zero elements). Solution: Make sure the array contains your model objects.

I might have left some other common mistake out... Let me know.


Addendum: Unless you have a compelling reason not to, I would strongly suggest that you use a stock UITableViewController instead of manually adding a UITableView to a plain UIViewController. You get a lot of functionality and setup for free.

Community
  • 1
  • 1
Nicolas Miari
  • 16,006
  • 8
  • 81
  • 189