I uitableview the (cell = nil) always not call? - ios

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

Related

Working with dynamic and static sections together in a tableview

I am having some problem to manage my cells in a tableview.
I have one table view with two sections:
The first one, I have 3 custom cells, with static cells.
The second one, I have a dynamic type.
To work with the dynamic one, no problem is occurring, but the static, I don't know how to "reuse" them.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexpath.section = 0){
//here is the question, how to return the stactis that had already been defined
}else{
return cell //here is okay, i can return the dynamic
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexpath.section = 0){
//here is the question, how to return the stactis that had already been defined
static NSString *CellIdentifier = #"StaticCell";
StaticCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return cell;
}else{
static NSString *CellIdentifier = #"DynamicCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}

uitable view inside custom cell

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.

Table View not populating objects

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"GenusNameCell";
GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GenusNameCell"];
cell.GenusNameLabel.text = [genusName objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
return cell;
}
I have a tableview with an Array of objects but when I run it. Nothing shows up. Im fairly new to xcode but Im not sure what my mistake is in the code. Can some one help me out?
If the cellForRowAtIndexPath callback is never called, it could be:
you didn't have set the dataSource of your tableview;
you didn't implement the numberOfSectionsInTableView: and/or tableView:numberOfRowsInSection: callbacks.
And if you dequeue your cell, you need to set the text outside the if branch like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"GenusNameCell";
GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GenusNameCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell.GenusNameLabel.text = [genusName objectAtIndex:indexPath.row];
return cell;
}
Change the code like this your code will run,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"GenusNameCell";
GenusNameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[GenusNameCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"GenusNameCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell.GenusNameLabel.text = [[genusName objectAtIndex:indexPath.row] geniusname];
return cell;
}
geniusname is the NSString you stored the name of the person.

Can't get my custom UITableViewCell to show

I am using a TableView xib and all the delegates and datasource seem to be running fine.
If I set self.textLabel.text, it displays the generic number of tableviews correctly, but I need to have my custom TableViewCell showing.
I created a HistoryCell.xib that has just a tableviewcell in it.
I created a UITableViewCell class "HistoryCell.h/HistoryCell.m" and it is set as the file owner of HistoryCell.xib.
I connected the UILabels to the HistoryCell.h
UILabel statusLabel
UILabel nameLabel
UILabel timeLabel
In my main ViewController class int he cellForRowAtIndexPath
I am putting in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"historyCellType";
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[HistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.nameLabel.text = #"Donald Duck";
return cell;
}
What am I doing wrong?
Thanks!
You should register the nib like this (probably in viewDidLoad):
[self.tableView registerNib:[UINib nibWithNibName:#"HistoryCell" bundle:nil ] forCellReuseIdentifier:#"historyCellType"];
Then in your cellForRowAtIndexPath method, use this new way where you don't need the if cell == nil clause:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:#"historyCellType" forIndexPath:indexPath];
cell.nameLabel.text = #"Donald Duck";
return cell;
}
You need to deserialize the actual XIB data:
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *cellnib = [[NSBundle mainBundle] loadNibNamed:#"HistoryXIBName" owner:self options:nil];
cell = (HistoryCell *)[cellnib objectAtIndex:0];
}
Otherwise it's just using a "Default" cell.
EDIT: Also, if you are using storyboards, dynamic cell prototypes remove the need to create cells from NIB files (in case that's an option for you.)
EDIT The 2nd:
You can try this as well (this assumes you are using ARC):
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
HistoryCell *aNewCell = [[HistoryCell alloc] initWithNibName:#"HistoryXIBName" bundle:nil];
cell = (HistoryCell *)*aNewCell.view;
}
There's another method using outlets to your cell in the containing view's XIB file that's a bit more involved that I used to use when iOS4 was current. If the edited version isn't' working for you, I'll dig out an old project and remember how I did it.
When you use tableViewCell with xib, while instantiation load from xib.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"historyCellType";
HistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:#"HistoryCell"
owner:nil
options:nil]lastObject];
}
cell.nameLabel.text = #"Donald Duck";
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellidentifier=#"cell1";
customcell *cell =
(customcell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (cell==nil) {
[tableView registerNib:[UINib nibWithNibName:#"Customcell" bundle:nil]
forCellReuseIdentifier:cellidentifier];
cell =
(customcell*)[tableView dequeueReusableCellWithIdentifier:cellidentifier
forIndexPath:[NSIndexPath indexPathForItem:indexPath.row
inSection:indexPath.section]];
}
return cell;
}

IOS: delegate methods for two tableView

myaI have this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == firstTableView){
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease] ;
}
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
return cell;
}
}
I check if tableview is firsttableview, but it give me a warning because the method haven't a "return" cell, how can I solve?
You only return a cell if the table is firstTableView. Make sure you return a cell for other tables by adding a return statement outside of your conditional.
Your code needs to return a value for all paths through that method. So, if your check for firstTableView fails, you still need to return a valid UITableViewCell from the method. You should probably read the UITableView programming guide - it walks you through proper usage of a tableview.

Resources