I am using a table view inside custom cell.The below code specifies the table inside custom cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"commnetCell";
commnetCell *cell = (commnetCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure Cell
[cell.lbl_comment setText:#"kool"];
return cell;
}
here cell returns nill.I have set the delegate and datasource.
You have to allocate the cell if there is none in the reuse queue.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Configure the cell.
return cell;
}
Alternatively, you can create a prototype in the storyboard or register a custom cell class. Then, you don't have to allocate it on your own. It will be created for you when you request it using the dequeueReusableCellWithIdentifier method.
The dequeueReusableCellWithIdentifier: will always return a cell if you have called registerClass: with the corresponding identifier.
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.
in the ui tableview cell,
the statement if (cell == nil) is not called.
Because when i initialize the cell outside the if statement, the talbe will have some wrong label in wrong row.
how can I initialize my cell?
The #"tttestttt" NS LOG is not all the time when the table is created.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"GroupsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
NSLog(#"tttesttttt");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
}
Use the more convenient method dequeueReusableCellWithIdentifier:forIndexPath: which returns always a valid cell.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"GroupsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier
forIndexPath:indexPath];
...
return cell;
}
dequeueReusableCellWithIdentifier: always return a cell if you're using Storyboard.
make Identifier not a fixed value
make it as index.row (convert to string as identity)
your wrong may be crate by cell refuse
I have XIB that contain UITableView and in same XIB i created UITableViewCell, I did not set any class to cell, I just set identifier to the cell.
Now I want to load that cell to tableview delegate methd cellForRowAtIndexPath
i did is :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
return cell;
}
but it create new cell that doesn't include xib that i created.
So how do i get that cell from same xib that table view has ?
If you are creating a custom cell, then it will be better if you create custom classes of UITableViewCell type and then instantiate your UITableViewCell class in method.If you dont want to create custom class then below is the code that can help you:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = #"SimpleTableItem";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil)
{
cell = (UITableViewCell *)[[[NSBundle mainBundle] loadNibNamed:#"TMPartnerLoyalty" owner:self options:nil] objectAtIndex:0];
}
return cell;
}
Here, TMPartnerLoyalty is name of the xib.
please write this code in viewdidload , i hope it will work for you.
NSString *nibName = #"give your xib name here that contain Tablecell";
NSString *simpleTableIdentifier = #"SimpleTableItem";
UINib *customCellNib = [UINib nibWithNibName:nibName bundle:nil];
[tableView registerNib:customCellNib forCellReuseIdentifier:simpleTableIdentifier];
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:#"cell"];
if(cell == nil)
{
cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"";
return cell;
}
If you use UITableViewcell(Custom cell),follow the below steps
1.in viewDidLoad you must register the UITableViewCell or Custom Cell
UINib *cellNib = [UINib nibWithNibName:#"YourUITableViewCell" bundle:[NSBundle mainBundle]];
[self.tableView registerNib:cellNib forCellReuseIdentifier:#"cell"];
2.OR in cellForRowAtIndexPath you can write the code below like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *customTableIdentifier=#"cell";
YourUITableViewCell *cell=(YourUITableViewCell*)[tableView dequeueReusableCellWithIdentifier:customTableIdentifier];
if (cell==nil)
{
NSArray *nibs=[[NSBundle mainBundle]loadNibNamed:#"YourUITableViewCell" owner:self options:nil];
cell=[nibs objectAtIndex:0];
}
cell.yourTableViewCellLabel.text = #"";
cell.yourTableViewCellTextField.text = #"";
cell.yourTableViewCellButton.text = #"";
return cell;
}
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;
}
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.