I have a custom UITableViewCell defined in a xib. It has two views in its Content View, a label and a text view.
In my table view controller, using either
JJDTextInputCell *cell = [[SDLTextInputCell alloc] init];
or
JJDTextInputCell *cell = [tableView dequeueReusableCellWithIdentifier:#"TextInputCell" forIndexPath:indexPath];
the IBOutlet is null, so the table view does not show the default text or any text I try to display using
cell.descriptionLabel.text = #"foo";
What is the proper way to initialize custom UITableView cells created using interface builder?
AFAIK, there are two ways to dequeue table cell, you can find them here in the doc. Find them under Creating Table View Cells. You are using dequeueReusableCellWithIdentifier:forIndexPath: which always returns a valid cell BUT you need to use registerNib:forCellReuseIdentifier: function in pair with this. You have to register the class and then you will get a valid cell. Check out this question for reference.
If you are using XIB for custom cell you should use
[self.tableView registerNib:[UINib nibWithNibName:#"TableViewCell" bundle:nil] forCellReuseIdentifier:"CellIdentifier"];
for registering a cell instead of
[self.tableView registerClass:[TableViewCell class] forCellReuseIdentifier:"CellIdentifier"];
Initialize them from xib file.
JJDTextInputCell *cell = [[[NSBundle mainBundle] loadNibNamed:#"JJDTextInputCell" owner:self options:nil] objectAtIndex:0];
Related
Every time VC is showed - UITableView loads cells from storyboard. And then successfully reuse them while scrolling. But every time showing controller - performes loading from storyboard. I would like to avoid this behavior.
Is there any way to reuse cells between UITableView instances this way?
You can't achieve that using storyboards. You will have to create a XIB file with a custom UITableViewCell. You can then reuse it:
UITableViewCell* customCell = [tableView dequeueReusableCellWithIdentifier:#"CellId"];
if(!customCell)
{
customCell = [[NSBundle mainBundle]loadNibNamed:#"UITableViewCellXibName" owner:nil options:nil][0];
}
I made a custom table view cell - i have a header, implementation, and nib. In the nib I set the style to custom, dragged a label on it and made an outlet in the nibs file owner.
From my UITableView Controller I have this code:
static NSString *CellIdentifier = #"adbActivityCell";
adbActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
//cell =[[adbActivityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.price.text = [NSString stringWithFormat:#"cell #%d", indexPath.item + 1];
return cell;
If I run this as is XCode tells me that the UITableView Controller is not key value compliant for the label property (the label is named "price"). If I comment out the two lines above and uncomment that one line my application runs, but the label doesn't show up at all, even if I set default text for it.
I've spent quite a lot of time researching tutorials and questions on here with no luck.
Its all about view hierarchy.
You have to add your label outlet to the custom UITableViewCell, because it is the superView of your label in view heirarchy.
That means label is contained in custom cell thats why you have add outlet to custom cell.
self.view->tableView->CustomCellView->UILabel
In your customCell.h file set the IBOutlet of the label. Your problem will be solved.
Is there an easy way of having a tableview cell like we see here with numbering like this and the border around. Is this created using different sections?
You need to create a custom UITableViewCell.
If you're using storyboards look here:
See this link http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/
If not here is a rundown:
Basically create a new class that inherits from UITableViewCell and a XIB. Drag a UITableViewCell to the XIB and set it to the class that you created previously.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = #"CustomCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
//*- Load your custom XIB. objectAtIndex:0 says load the first item in the XIB. Should be your UITableViewCell that you dragged onto your XIB in Interface Builder.
cell = [[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil] objectAtIndex:0];
}
//*- Customize the cell, i.e., cell.myLabel.text = #"Text";
return cell;
}
Using this technique you can layout your cell with three labels, one for the number and one for the name of the song and one for the song time. Add a background image view for the border and color.
A simple way to get the song number in the table is to use the indexpath.
cell.myLabel.text = [NSString stringWithFormat:#"%i", indexPath.row + 1];
I have a TableView that i populate with custom cells. In this code i would like to set the data on my different labels, but it doesnt seem to work. I can not add any outlets, not sure why i cant do that. When i try to change the data with cell.textLabel.text = #"Data"; it seems to add a new label instead of changing the text of the current one.
What should i do? Code below shows how i populate the list.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SenasteTableCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
if(cell == nil)
{
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"SenasteTableCell" owner:nil options:nil];
for (UIView *view in views)
{
cell = (SenasteTableCell*)view;
}
}
return cell;
}
Set a unique tag to the label in customcell and you can get its instance as
UILabel *Label=(UILabel *)[cell viewWithTag:2];//2 is th unique tag value i set in the cell for that label
You can get value for every view objects in the cell like this
Eg
UIButton *sampleButton=(UIButton *)[cell viewWithTag:4];
You should be able to create outlets. If you are using a custom table view cell (which Im assuming SenasteTableCell is) make sure that in the interface builder you click on the cell and set its class to SenasteTableCell. At that point you should be able to control drag from your labels to the file and create outlets.
I think you have not set the FileOwner Property form XIB file.
set fileowner as UITableViewClass and UITableViewCell As SenasteTableCell.
To connect outlets you have to first select SenasteTableCell and there will be a list of oulets.
I am using AQGridView class and I am trying to load a cell from an XIB. I have setup the XIB like a Custom Cell for a UITableView, but when I attempt to load the cell, it is simply blank. I was wondering if there was an easier way to get the XIB to load.
AQGridViewCell need to load the cell from an xib
- (AQGridViewCell *) gridView: (AQGridView *) gridView cellForItemAtIndex: (NSUInteger) index
{
static NSString * CellIdentifier = #"cellID";
gridCell * cell = (gridCell *)[gridView dequeueReusableCellWithIdentifier: CellIdentifier];
if ( cell == nil ){
gridCell = [[gridViewCell alloc] initWithFrame: CGRectMake(0,0,_gridView.frame.size.width/2-4,
_gridView.frame.size.height/2-8)
reuseIdentifier:CellIdentifier];
cell = gridCell;
self.gridCell = nil;
}
cell.title = #"Test Grid Item";
cell.date = #"Apr. 7, 2011";
return ( cell );
}
Here's an article that describes how to load an AQGridViewCell from nib, with example code. Check out the section called "A reusable AQGridViewCell".
(Thanks to pt2ph8 for pointing out contentView.)
From what I've understood, I think it shows as blank because what gets displayed is the cell's contentView. I ended up loading my custom view from IB and adding it as a subview of the cell's contentView when the cell is requested.
AQGridView's developers once claimed on GitHub that proper IB support will be added in the future, but that post is dated August 2010, so don't hold your breath.
This took me a while, but I figured a different way than the blog post jlstrecker mentioned.
Create a subclass of AQGridViewCell - let's call it
MyGridViewCell.
Create a nib for that cell, link it up in IB.
Pub a view ON TOP of the cell's view in IB. That's right, a view
on top of a view. Make the size the exact same.
For that view on
top of the view (let's call it view2), set the tag property (can
be done in IB) to 1.
Put everything you want to link up on top of
view2, decorate your cell, whatever you'd like.
Use the following code (of course, change it to your needs) in your subclass of AQGridViewController:
`
- (AQGridViewCell *)gridView:(AQGridView *)aGridView cellForItemAtIndex:(NSUInteger)index {
static NSString *CellIdentifier = #"MyGridViewCell";
MyGridViewCell *cell = (MyGridViewCell *)[self.gridView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = (ZZProductGridViewCell *)[[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
}
[cell.contentView addSubview:[cell viewWithTag:1]]; //THIS IS THE IMPORTANT PART
return cell;
}
Enjoy!
I'm not familiar with AQGridView, but I believe you can leverage NSBundle's Nib loading capabilities. An excerpt from AdvancedTableViewCells sample project illustrates the idea:
RootViewController.h
#interface RootViewController : UITableViewController
{
ApplicationCell *tmpCell;
}
RootViewController.m
ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"IndividualSubviewsBasedApplicationCell" owner:self options:nil];
cell = tmpCell;
self.tmpCell = nil;
}
Inside the IndividualSubviewsBasedApplicationCell.xib you would have to set the outlet of the UITableViewCell within to be the RootViewController's tmpCell property. Then, as a side effect of invoking NSBundle's loadNibNamed method, the tmpCell property gets set on the RootViewController via the Nib loading mechanism.
What you can do is do your xib (uiview) unpacking/loading in the subclass itself (which does have a different init method than a uitableviewcell)
you can also connect any outlets to this xib and add its entire view as a subview, or maybe replace contentview).
To make it even faster you can make uinib of this xib and reuse it to save disk i/o.
Build your cell normally using IB, then in your subclass of AQGridViewCell, add
- (void)awakeFromNib{
self.contentView.backgroundColor = [UIColor clearColor];
}