I cannot get the content from my custom cell's xib file, the cells are empty, the cell .xib file only contains a UILabel, then the code in the Master class (in a Master-Detail project) is as follow :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
//[self configureCell:cell atIndexPath:indexPath];
return cell;
}
Where self configureCell is for Core Data, to test my UILabel, I did not want to get the value from Core Data, so I commented this line.
The "custom class" is set to "MyCell" in the storyboard, the identifier is the same one...
I also tried this in my class MyCell:
- (void)awakeFromNib {
// Initialization code
self.ibLabel.text = #"allo";
}
but my cells are still empty... The height of each cell is also the same in the .xib as in the code...
EDIT #1:
This worked to register the cell custom class:
UINib *cellNib = [UINib nibWithNibName:#"MyCell" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:#"Cell"];
Make sure you register the nib in viewDidLoad:
self.tableView.registerNib(UINib(nibName: "MyCell", bundle: nil), forCellReuseIdentifier: "Cell")
You have to initialize the cell. It might be the first cell and you haven't got one before...
This method dequeues an existing cell if one is available or creates a new one based on the class or nib file you previously registered.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell;
cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
if (cell == nil)
{
cell = [[MyCell alloc] init];
cell.ibLabel.text = #"allo";
}
return cell;
}
Why not do you try in the cell forRow:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = (MyCell*)[tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.ibLabel.text = #"allo";
return cell;
If this don't work, you must be sure that "Cell" is also the identifier in the storyboard, and in the storyboard the class is MyCell.
First, a stupid question: Have you implement this method and It's not returning 0 ?
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5; // To test
}
Second:
In storyboaard change de backgroundColor of your custom cell, in order to be sure that is this cell painting.
Related
My table simply has row with a UIImageView. When I assign the image directly like the below mentioned way
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier = #"cellidentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
// Configure the cell...
cell.imageView.image = [self.backgroundImages objectAtIndex:indexPath.row];
return cell;
}
It returns error:
unable to dequeue a cell with identifier cellidentifier - must
register a nib or a class for the identifier or connect a prototype
cell in a storyboard'.
So I wonder is there a way to ignore to register a nib or a class for UITableViewCell in order to shorten the code in this case?
If you are using custom class for your cell then you should register the class (in viewDidLoad),use registerNib:forCellWithReuseIdentifier: if the cell is made in a xib file.
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
else write cellForRowAtIndexPath tableView delegate as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//Configure cell
return cell;
}
You just need to add one line in your viewDidLoad to resolve the error
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"cellidentifier"];
}
Put your cell identifier in storyboard as below screenshot.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Songcell"; // copy this identifier and paste it into your storyboard tableviewcell indetifier
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UIImageView *IMAGEVIEWNAME = (UIImageView*)[cell viewWithTag:YOUR_TAG]; // give tag to your imageview from storyboard and access it here
IMAGEVIEWNAME.image = [UIImage imageNamed:#"YOURIMAGENAME"];
return cell;
}
Give same name in your storyboard tableviewcell identifier
cellidentifier with this name . i just give my cell identifier songcell.
I want use registerNib: forCellReuseIdentifier: method in my app
when i was made custom cell with xib file is fine. (ex:SwitchTableViewCell2.xib )
because file name is xib name.
working well code:
[self.tableView registerNib:[UINib nibWithNibName:#"SwitchTableViewCell2" bundle:nil] forCellReuseIdentifier:#"xibcell"];
but did not working when i made prototypecells on tableview in mainStoryboard.
how to get xibname of prototypecell on storyboard?
anybody can test this link: github.com/Kernelzero/test_nav
add image files here
this controller name : 'PushSettingController'
this controller's storyboard id: 'pushSetting'
If you use prototype cell,Identifier name must same as dequeueReusableCellWithIdentifier name
NOTE:If you use prototype cell, you don't need to register the cell.Because Prototype cell is not a CustomCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"switchcell"];
if(cell == nil)
{
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"";
return cell;
}
But if you use custom cell,follow the below steps
1.in viewDidLoad you must register the custom cell
UINib *cellNib = [UINib nibWithNibName:#"SwitchTableViewCell2" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:cellNib forCellReuseIdentifier:#"switchcell"];
2.OR in cellForRowAtIndexPath you can write the code below like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customTableIdentifier=#"switchcell";
SwitchTableViewCell2 *cell=(SwitchTableViewCell2*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier];
if (cell==nil)
{
NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:#"SwitchTableViewCell2" owner:self options:nil];
cell=[nibs objectAtIndex:0];
}
cell.yourSwitchTableViewCell2Label.text = #"";
cell.yourSwitchTableViewCell2TextField.text = #"";
cell.yourSwitchTableViewCell2Button.text = #"";
return cell;
}
Take the registerNib call out when using the Storyboard. If you've set the storyboard up correctly, the prototype cells are already registered to the tableView.
There's no need to load it from nib. Call:
Objective-C
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"switchcell];
Swift
let cell = tableView.dequeueReusableCellWithIdentifier("switchcell")
I have a table view with a custom protoype cell and the cell has 3 different labels on it. How do I get access to these labels? I need to change their texts. I've searched around everywhere and haven't found something that helps me in this case. I have the below code:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"basicCell" forIndexPath:indexPath];
//change cell's 3 labels' text here
return cell;
}
How can I access the above cell's 3 different labels that it has?
You need to make a class that is a subclass of UITableViewCell, and set the custom cell's class to the class you made. Then you want to link the labels using IBOutlets in the cell to the subclass you made.
Finally, in -tableView:cellForRowAtIndexPath:, you should cast the cell to your new subclass so you get access to those IBOutlets. Assuming your outlets are named someLabelOutlet1, someLabelOutlet2, and someLabelOutlet3, do the following after you finished subclassing.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SomeTableViewCellSubclass *cell = (SomeTableViewCellSubclass *)[tableView dequeueReusableCellWithIdentifier:#"basicCell" forIndexPath:indexPath];
cell.someLabelOutlet1.text = #"sometext"
cell.someLabelOutlet2.text = #"sometext"
cell.someLabelOutlet3.text = #"sometext"
return cell;
}
In cellForRowAtIndexPath, you can access these labels
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
if (!cell) {
[tableView registerNib:[UINib nibWithNibName:#"CustomCell" bundle:nil] forCellReuseIdentifier:#"CustomCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"CustomCell"];
// set common properties here
cell.lbl1.textColor=[UIColor whiteColor];
cell.lbl1.backgroundColor=[UIColor redColor];
cell.lbl1.font=[UIFont fontWithName:#"Verdana" size:16.0];
}
cell.lbl1.text=#"lbl1Txt";
cell.lbl2.text=#"lbl2Txt";
cell.lbl3.text=#"lbl3Txt";
return cell;
}
I'm following this tutorial for creating a custom UITableViewCell, and in the object returned from from [tableView dequeueReusableCellWithIdentifier:#"ID"], all of its UILabels and etc. are nil. This tutorial doesn't say anything about creating them myself, and I would expect I wouldn't need to, judging by the way the rest of iOS works, but I don't know why they aren't being created automatically. For example, here cell.lightName is nil, even though it is hooked up to the label that exits in the custom view in IB.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *dataCellIdentifier = #"Data Cell";
AnalysisHistoryTableViewCell *cell = (AnalysisHistoryTableViewCell*)[tableView dequeueReusableCellWithIdentifier:dataCellIdentifier forIndexPath:indexPath];
self.selectedAnalysis = self.analysisResults[indexPath.row];
cell.lightNameLabel.text = self.selectedAnalysis.lightName;
return cell;
}
What am I doing wrong thats causing my UILabels to not exist?
With a custom cell you'll need to register the nib first:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:#"AnalysisHistoryTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:#"Data Cell"];
}
Then in your cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AnalysisHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Data Cell"];
//etc
return cell;
}
Recently I noticed that when we creates a new Class template, (Xcode -> File -> New -> File) which is a subclass of UITableViewController implementation is partial in cellForRowAtIndexPath method. I pasted the method below.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
return cell;
}
I think statements like (which is missing),
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
is must in implementation. Is this a bug or something else ?
You don't need to create a cell if you are registering a cell in your table view with the method registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: or creating a cell within your table view in a xib or storyboard.