I am using a tableview inside a uiviewcontroller inside a XIB. Because the controller is not a UItablecontroller, I cannot configure the cell directly. If I drag in a tableview cell, i can configure that with the cell identifier etc,but the XIB doesn't use my tableview cell. How do I join them up?.I would rather not use code as this seems counterintuitive to the workflow.
-
is it a static or dynamic tableview? You may be looking for a static table view... – jackslash Feb 26 '13 at 10:13
-
its not a static cell – angel of code Feb 26 '13 at 10:54
-
in that case you are going to have to code to make it work. The xib is great for laying out and adding subviews. Filling the cells is programmatic – jackslash Feb 26 '13 at 11:26
2 Answers
If you are using Storyboards you can directly add custom cells within the VC and configure them. But if you are using xib's you have to either create a custom cell class and load them programmatically or create a custom class with custom cell in separate xib and then load the custom cell from xib, or even you just create a xib with a simple custom table cell and load from xib. Refer apple Docs on UITableView
So as I understood, you want a custom cell in your code. If you are not using storyboards, try using this. Here I have created a custom class for custom cell and added the custom cell in xib. I added a separate xib, dragged and dropped UITableViewCell in xib(remove any views if present). Now click on the cell you just added, in the identity inspector change the class name to that of the custom class you created. Select files owner and change its class to the name of class where you need your custom cell. (This tells thats your cell will be used in that particular class where the tableview's delegate and datasource is connected). Now in the class where you want your cell, add a IBOutlet of type UITableViewCell. Connect this to the cell you created in xib.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"CustomCell";
YourCustomCell *cell = (YourCustomCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell)
{
[[NSBundle mainBundle] loadNibNamed:@"YourCustomCell"
owner:self
options:nil];
cell = self.yourOutletCell;
self.yourOutletCell = nil;
}
}
And if you are using xib's to load and if you have more than one xib's of custom cell, you can load them via the xib name like this
NSArray *topLevelObjects;
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"YourCustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (YourCustomCell *) currentObject;
break;
}
}

- 1,031
- 6
- 25
-
Thanks, I want to use a custom cell class, how do i load the custom cell class's XIB in code? Is there a tutorial link somewhere for more information? – angel of code Feb 27 '13 at 08:51
-
-
I need a custom tableview cell with a XIB loaded from a tableview inside a uiviewcontroller. i dont have code yet, but I know i can reference a custom class in code - how does it know which XIB to use? – angel of code Feb 27 '13 at 10:33
If it is a static Cells, you can point to this cell with Outlet and then return it in the cellForRowAtIndexPath

- 7,063
- 3
- 53
- 64
-
So you have to make a custom Cell. Make new class inherits from the `UITableViewCell` then change the class of the cell that is in xib file from `UITableViewCell` to the new Class. Create new cell inside `cellForRowAtIndexPath` with the new class. Note: I think that works only with storyboard, check for that – Hossam Ghareeb Feb 26 '13 at 11:12