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.
Related
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];
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 simple table with custom cells each containing a textfield. In cellForRowAtIndexPath: I create and initialize each cell depending on indexPath.row:
case 0:
{
CellIdentifier = #"TextEditCell";
TextEditCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
[cell configureCellWithText: [self.valueArray objectAtIndex:0]
placeholder: #"value no.0"]
[cell performAction: #selector(saveValue0:)
forControlEvent: UIControlEventEditingDidEnd
inTarget: self];
return cell;
}
configureCellWithText:placeholder: sets text and placeholder of cell's textField.
performAction:forControlEvent:inTarget refers directly to textField and saves the value of textField to local array to be accurate when used again.
Problem occurs, when I scroll the table fast. Values from different cells copy to another cells and modify local array. I can't find out why it happens. Anyone have any idea? I can provide more code if needed.
This is happening because you are reusing the cells and configureCellWithText is being run after the cell is reused. To solve this you could:
Don't reuse cells - But this would really hurt your performance.
If you are running on 6.0 you can use tableView:didEndDisplayingCell:forRowAtIndexPath: to cancel the text setting action when the cell scrolls off screen.
You can create a flag in your custom cell class that you set when you dequeue a cell.
Edit
Because I do not know how your cell works. It is hard for me to give you anything more then a sudo code concept.
Here is my sudo code:
Tableview Cell for row...
- dequeue cell
- [cell cancel_previous_action]
- set new actions.
I've done custom UITableViewCells before without issue.. but I can't figure out what is going on with my current project.
Here's what I've done...
Create CustomCell.h (subclassing UITableViewCell)
Create an empty XIB and drag in UITableViewCell. Set background to black.
Change class of UITableViewCell in Interface Builder to "CustomCell"
Import CustomCell.h in my DetailViewController
Modify tableView:cellForRowAtIndexPath:
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#"DO I GET HERE?");
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
I would expect my tableview cells to show as black? But they are still showing as white... any ideas? Thanks!
Update: Ok, turns out, it is loading the custom cell... I added a UILabel with some white text. I couldn't see it, but when I highlighted the cell I could see the text was there. So now the question becomes, why is the cell ignoring the black background I have set for the cell?
EDIT: As for why it's not black - I expect there is something obscuring your black - the likeliest candidate for this is the label background being white and not clear.
As well as point 3.
The attributes inspector (the 4th tab) needs to have the reuse identifier set to the identifier you are going to reuse (you use #"cell" in your question). I would try and use something a bit more specific - after all in some apps you might have many types of custom cells.
I think you also need to cast the topLevelObjects to (CustomCell*) thus
if (cell == nil) {
NSLog(#"DO I GET HERE?");
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:nil options:nil];
cell = (CustomCell*)[topLevelObjects objectAtIndex:0];
}
Seems to ignore the background colour I've set, so I just add a UIView to it with a background colour and that seems to work..
You have to register your custom cell with the tableview
This needs to happen before that delegate is called:
[self.tableView registerClass: [CustomCell class] forCellReuseIdentifier:#"CellIdentifier"];
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];
}