Having trouble with UITableViewCell loaded from nib - iphone

I am having trouble wiht UITableViewCell loaded from nib. I used apple documentation to that but I do't know where I am going wrong. UITableViewCell created through IB contains 5 UIlable. Nib are loaded perfectly but the problem is when I scroll text of the label changes automatically to different.
Following is the my code for cell for row at indexpath. Please let me know where I am going worng.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowNo = [indexPath row];
NSLog(#"Row No:%d",rowNo);
Transfer *tempTransferRecord =(Transfer*)[self.transferInformation objectAtIndex:rowNo];
seasonYear.text=tempTransferRecord.seasonYear;
longName.text=tempTransferRecord.longName;
transactionDate.text=tempTransferRecord.transactionDate;
toTeam.text=tempTransferRecord.toTeam;
fromTeam.text=tempTransferRecord.fromTeam;
/*
static NSString *MyIdentifier = #"MyIdentifier2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier] ;
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:#"PlayerTransfers" owner:self options:nil];
cell=transfersInfoCell;
self.transfersInfoCell=nil;
}
*/
static NSString *CellIdentifier = #"MyIdentifier2";
static NSString *CellNib = #"PlayerTransfers";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
// perform additional custom work...
return cell;
}

Try moving your cell loading code to the top of cellForRowAtIndexPath, and the rowNo and value setting after that. Cells changing content must mean you're either changing what's in the data source between when you first set it and when you scroll, or you're setting values at the wrong point in time (i.e setting values on row 5 but then loading row 6 from a nib, or setting values on a row but then loading the nib for it afterwards so the values you set are overridden). Also, I'm a bit concerned that you set local variables rather than, say, (PlayerTransfers*)cell.seasonYear.text = tempTransferRecord.seasonYear or (UILabel *)[cell viewWithTag:1].text = tempTransferRecord.seasonYear. Shouldn't you be setting the values within each cell rather than on your table view class? That could explain why values are changing to something different: you may use the same variables to set every cell.

Have you set a CellIdentifier in Interface Builder?

Related

setting UILabel.text of a custom UITableViewCell inside ViewController

I have a UITableView that I am populating with custom UITableViewCell in interface builder. I am having some issues accessing these custom tableviewcells properties and am hoping for some help.
In Interface Builder I am setting the custom tableviewcell's class to the current View controller (so I can assign all of the label objects to the correct labels in Interface Builder), So I have also set up the IBOutlet labels to the correct labels in Interface Builder However this error occurs when I try to pass the NSString from the array object variable (which is of type NSString) to the UIlabel's text.
Property 'cellDescription' not found on object of type 'UITableViewCell *'
Below is the code I have used to set up my tableview with the custom tableviewcell and then try to populate the cells UILabels with the correct text..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.section == 0) {
// Set cell height
[self tableView:tableView heightForRowAtIndexPath:indexPath];
// Configure the cell using custom cell
[[NSBundle mainBundle] loadNibNamed:#"AutomotiveSearchCell" owner:self options:nil];
cell = autoSearchCell;
//call dataArrayOfObject that has all of the values you have to apply to the custom tableviewcell
SearchResultItem* myObj = (SearchResultItem*)[dataArrayOfObjects objectAtIndex:indexPath.row];
cell.cellDescription.text = myObj.seriesDescription; // This is where I am receiving the error
NSLog(#"%#", myObj.seriesDescription); // This logs the correct value
//Disclosure Indicator
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
you must typeCasting UITableViewCell To AutomotiveSearchCell
I think you code somewhere is strange(There is no declaration for autoSearchCell), but you must do the following.
cell = (AutomotiveSerachCell* )autoSearchCell;
The above code does not work, should following code.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
convert to
AutomotiveSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
If that does not work above, refer a following process.
make a CustomCell Class.
make a CustomCell xib.
linked label to CustomCell Class.
import header #import "AutomotiveSearchCell.h" and following code copy and paste.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
AutomotiveSearchCell *cell = nil;
if(!cell)
{
UINib *nib = [UINib nibWithNibName:#"AutomotiveSearchCell" bundle:nil];
NSArray *arr = [nib instantiateWithOwner:nil options:nil];
cell = [arr objectAtIndex:0];
cell.cellDescription.text = #"Test~!~!~!";
}
// Configure the cell...
return cell;
}

UITableView value keep changing while moving

I have configured a table view cell, whose tables cell is loaded from another xib, both the filename and the identities are called,BlogViewControllerTableCell
Most of them works properly, but some contents in the cells keep changing while scrolling.
No matter I use the array value, or simply put the [indexpath row].
The below is the code,
I would be appreciate for your help.
Thanks.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"BlogViewControllerTableCell"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"BlogViewControllerTableCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
}
student_name.text = [NSString stringWithFormat:#"%#", [[student_list objectAtIndex:[indexPath row]] objectForKey:#"name"]];
return cell;
}

tableView cellForRowAtIndexPath called multiple times initially

I am trying to understand how the rendering of each cell works. Here's my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"QuestionCell";
static NSString *cellNib = #"QuestionsTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
//cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellNib owner:self options:nil];
cell = [nib objectAtIndex:0];
NSLog(#"%#", #"--> load from nib");
}
return cell;
}
The NSLog in this code is called 8 times initially (because I have 8 rows visible initially). Then when I start scrolling, it's called once and then no more after. I thought it should've been called once and that's it since it should be re-using it after the first call? And why is it called once more after the initial 8? (scratching head...)
It's possible that there could be 9 cells on screen when you are scrolling the table view. This wasn't needed when you started but as you scroll, the 9th cell is required as the top one leaves, a new cell at bottom enters. A cell can't be reused in halves. :)

Using multiple custom cells without reusing identifier

I feel like a real noob asking this, but here's my problem:
I want to show a tableView, with 7 custom cells. None of these cells is reused. That means the user will see 7 different cells, not more, not less.
I created the cells in the viewDidLoad method, and added all those cells in the listCells-array. After that, I used easy-mode to draw those cells:
UITableViewCell *cell = nil;
if (indexPath.row == 0)
{
static NSString *MyIdentifier = #"Cell";
cell = (DetAlertCell *)[localTableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"DetAlertCell" owner:self options:nil];
cell = [listCells objectAtIndex:indexPath.row];
}
}
....
However, this won't work. It shows me a blank view. Every cell is created using a .xib-file and a .h and .m class. Is there anything that I'm missing and should do?
Just don't call the [localTableView dequeueReusableCellWithIdentifier:nil] and loa the correct cell for the index path.
Also you say that you load the cells in the viewDidLoad, then why do you load the nib:
[[NSBundle mainBundle] loadNibNamed:#"DetAlertCell" owner:self options:nil];
They should already be the array should they not.
And why to you check if the row is 0 then load the row, still will only load the first row.
Try this:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [listCells objectAtIndex:indexPath.row];
}

how to make a UIView added to UITableViewCell content view fit within its bounds?

how to make a UIView added to UITableViewCell content view fit within its bounds?
That is, I have created a single NIB file (with 3 labels on it), and want to use this for the display of each cell in a UITableView. I'm adding in the cellForRowAtIndexPath method the custom NIB based view to the cell's content view, however all I see as an end result is one (1) custom NIB based view (not multiple within a tableview as I expected).
How do I arrange so that each custom view fits neatly within each UITableViewCell? Also note the labels within the custom NIB view have word wrap.
Do I have to create a frame for the custom NIB view, but then in that case I'm not exactly sure how to set the co-ordinates. Would it be relative to the UITableViewCell? and if the custom view height can change due to it's UILabel word wrap, then I assume I separately have to manually calculate this height in the UITableView? (perhaps I should go to dynamic/custom view creation and drop the concept of using InterfaceBuilder/NIB)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIView *detailedView = [[[NSBundle mainBundle] loadNibNamed:#"DetailedAppointView" owner:self options:nil] objectAtIndex:0];
[cell.contentView addSubview:detailedView]; // DOESN'T SEE TO WORK
return cell;
}
This is how I do it if I need to implement a custom UITableViewCell.
I create a subclass of UITableViewCell for my custom cell ex. "MyCustomCell". Then I create a NIB file for it, and within this NIB file I insert UITableViewCell and change it's type to "MyCustomCell" and I give it an identifier with the same name as the class name. Then I insert all my subviews onto my cell. All in IB.
After I got my cell pimped out to my likning :-) I use it in the following way:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCustomCell";
static NSString *CellNib = #"MyCustomCell";
MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (MyCustomCell *)[nib objectAtIndex:0];
}
//Manipulate your custom cell here
return cell;
}