Iphone: CustomCell, can't set data - iphone

I try to make a custom cell with a Uilabel and textfield
And i get this:
-> http://data.imagup.com/9/1119535256.png
But it's not given given my value ("Titre" & "Valeur" are generic):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"InscriptionCustomCell";
InscriptionCustomCell *cell = (InscriptionCustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"InscriptionCustomCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (InscriptionCustomCell *) currentObject;
break;
}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if(indexPath.section ==0)
{
[cell.titreCell setText:[model.listModuleInfoPerso objectAtIndex:indexPath.row]];
[cell.contenuCell setPlaceholder:[model.listModuleInfoPerso objectAtIndex:indexPath.row]];
}else {
[cell.titreCell setText:[model.listModuleInfoSupp objectAtIndex:indexPath.row]];
[cell.contenuCell setPlaceholder:[model.listModuleInfoSupp objectAtIndex:indexPath.row]];
}
return cell;
}
No error :S, an idea?

Put an NSLog in the [model.listModuleInfoPerso objectAtIndex:indexPath.row] so you can see what is coming out...

You probably forgot to hook up the label and the text field from Interface Builder to the properties in your InscriptionCustomCell subclass.

Here it seems that you haven't set the delegate for UITextField. So, please set delegate now and then you can access the TextField entry.

Related

custom label value is disappearing when scrolling table view

I am new to iphone. my cutomcell label value is disappearing when I scroll the table view.
Again it appears when I click on that cell.
Can anyone help me?
Thanks in Advance.
//table view in view controller created in xib
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListOfProductsCell";
ListOfProductsCell *cell = (ListOfProductsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
NSArray *nib =[[NSBundle mainBundle] loadNibNamed:#"ListOfProductsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
productItemDit=[productListArry objectAtIndex:indexPath.row];
NSString *offerStr= [NSString stringWithFormat:#"%.2f",[[productItemDit objectForKey:#"offer"] floatValue]];
NSString *fullCostStr=[[currencyCodeStr stringByAppendingString:#" "] stringByAppendingString:offerStr];
NSLog(#"%#",fullCostStr);
cell.itemCostLbl.text=fullCostStr;
} else {
cell.itemStepper = (UIStepper *) [cell viewWithTag:2];
cell.itemAddedLbl =(UILabel*)[cell viewWithTag:1];
}
if (tableView == self.searchDisplayProduct.searchResultsTableView) {
searchProductItemDit=[searchProductListArry objectAtIndex:indexPath.row];
NSLog(#"searchdit:%#",searchProductItemDit);
cell.itemNameLbl.text= [searchProductItemDit objectForKey:#"name"];
self.searchDisplayProduct.searchResultsTableView.separatorColor=[UIColor colorWithRed:200.0 green:0.0 blue:0.0 alpha:1.0];
} else {
productItemDit=[productListArry objectAtIndex:indexPath.row];
NSLog(#"dit:%#",productItemDit);
cell.itemNameLbl.text=[productItemDit objectForKey:#"name"];
}
cell.itemAddedLbl.text = [[NSString alloc] initWithFormat:#"%d",itemCount];
cell.itemImg.image = [UIImage imageNamed:#"profp.jpg"];
return cell;
}
Solved by doing like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListOfProductsCell";
ListOfProductsCell *cell = (ListOfProductsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
NSArray *nib =[[NSBundle mainBundle] loadNibNamed:#"ListOfProductsCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
} else {
cell.itemStepper = (UIStepper *) [cell viewWithTag:2];
cell.itemAddedLbl =(UILabel*)[cell viewWithTag:1];
}
if (tableView == self.searchDisplayProduct.searchResultsTableView) {
cell.itemNameLbl.text= [[searchProductListArry objectAtIndex:indexPath.row] objectForKey:#"name"];
} else {
cell.itemNameLbl.text=[[productListArry objectAtIndex:indexPath.row] objectForKey:#"name"];
}
cell.itemCostLbl.text=[NSString stringWithFormat:#"%# %.2f", currencyCodeStr , [[[productListArry objectAtIndex:indexPath.row] objectForKey:#"offer"] floatValue]];
cell.itemAddedLbl.text = [[NSString alloc] initWithFormat:#"%d",itemCount];
cell.itemImg.image = [UIImage imageNamed:#"profp.jpg"];
return cell;
}
As you haven't post much of the information this is my guess.when you scroll table view it internally calls the reloadData method . You need to use reuse Identifier to preserve the state of the cell. You can initialize the cell in cellForRowAtIndexPath like :
static NSString *cellIdentifier = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
// or cell initializing logic here for the custom cell
}
in case of custom cell add reuse identifier in the xib and use it in your cellForRowAtIndexPath method

Scroll down crashes application on a UITableView with a custom UITableViewCell because of zombie object

I have this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"BSTGListCell";
BSTGListCell *cell = (BSTGListCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
cell = [[[NSBundle mainBundle] loadNibNamed:#"BSTGListCell" owner:self options:nil] objectAtIndex:0];
}
PFObject* currentEl = [self.tableData objectAtIndex:indexPath.row];
cell.title.text = [currentEl objectForKey:#"Name"];
cell.description.text = [currentEl objectForKey:#"Address"];
return cell;
}
I am getting "message sent to deallocated instance" when scrolling down the table view which is added as a subview.
Zombie inspector says the accessed object is retained here:
cell = [[[NSBundle mainBundle] loadNibNamed:#"BSTGListCell" owner:self options:nil] objectAtIndex:0];
and probably released by ARC.
Why this happens and how I can prevent it?
You really shouldn't do it this way. The way to use cell from a nib is to register the nib, probably in viewDidLoad, like this:
UINib *nib = [UINib nibWithNibName:#"BSTGListCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:#"BSTGListCell"];
Then in your cellForRowAtIndexPath, use dequeueReusableCellWithIdentifier:forIndexPath: and no if(cell == nil) clause.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BSTGListCell *cell = (BSTGListCell *)[tableView dequeueReusableCellWithIdentifier:#"BSTGListCell" forIndexPath:indexPath];
PFObject* currentEl = [self.tableData objectAtIndex:indexPath.row];
cell.title.text = [currentEl objectForKey:#"Name"];
cell.description.text = [currentEl objectForKey:#"Address"];
return cell;
}
The actual problem with your code is that loadNibNamed:owner:options, returns an array, and you have to get the one object out of that array before you assign it to cell. But, the way I showed is a more efficient way to do this anyway.
My problem was something with the dequeueReusableCell line, i took it out like this link did and mine works now

Add image and text to tableview

I try to add some text taken from a textfield and an image taken from photo album to UITableViewCell.
In one UIViewController I have a UITextField and a UIImageView. This is the code I use to
populate an array of products that I would present in cells of a UITableView.
Article *article1 = [[Article alloc] initWithTitle: #"First Text" Subtitle: #"Data&Time" Thumbnail: [UIImage imageNamed: #"image.png"]];
articles = [[NSMutableArray alloc] init];
[articles addObject:article1];
Do you want the cells to display the properties of your Article objects? Let's assume you only have one section and your UITableViewController already has a reference to an array of Articles called "articles".
- (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];
}
Article *article = [articles objectAtIndex:indexPath.row];
cell.textLabel.text = article.title;
cell.detailTextLAbel.text = article.subtitle;
cell.imageView.image = article.thumbnail;
return cell;
}
You have to inherit UICell class, there are too many examples. Just google smth like "custom UITableView" or "custom UITableViewCell".
Also, there is left image for cell in Cocoa.
ADD a custom cell to display image to the table view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"MoreDetailCell";
MoreDetailCell *cell = (MoreDetailCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"MoreDetailCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (MoreDetailCell *)currentObject;
break;
}
}
}
cell.compImage.image = [UIImage imageNamed:#"Placeholder.png"];
cell.yourtexfieldname.text = #"ABC";
}

custom uitableviewcell

i building an chat app and i have a little problem, build cellforrowatindexpath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"UserCell";
UserCell *cell = (UserCell *)
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"UserCell"
owner:nil
options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (UserCell *) currentObject;
break;
}
}
}
NSString *nameValue = [self.names objectAtIndex:indexPath.row];
NSString *statusValue = [self.usersStatus objectAtIndex:indexPath.row];
cell.name.text = nameValue;
cell.status.text = statusValue;
return cell;
}
and in every cell i have a label what is hidden,and in:
-(void)chatHandlerNewMsg:(ChatHandler*)chat withInt:(NSString*)userId{
NSInteger tmp = [self.usersID indexOfObject:userId];
NSIndexPath *index = [NSIndexPath indexPathForRow:tmp inSection:0];
UserCell *cell = (UserCell*)[self.table cellForRowAtIndexPath:index];
[cell.newMsg setHidden:NO];
[self.table reloadData];
}
i check for new msg and if so i bring the uilabel from hidden to shown.
the problem is that the table view show this uilabel(that it is hidden by default) in other cells in the table view and not only in the specific one that we choose in :
UserCell *cell = (UserCell*)[self.table cellForRowAtIndexPath:index];
When ever you dequeue a cell you need to manually reset any state that a previous row may have set.
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell) {
//Reset cell state (make label hidden)
}
else
{
//Create new cell and configure (hide labels)
}
//Set any common attributes here (title)
return cell;

Use multiple cell classes in tableViewController

Is there a way to use multiple uitableviewcell classes with nib files in the tableViewController?
There's this great tutorial video for how to create custom cell classes to be used in a tableViewController at the cellForRowAtIndexPath function that I'm pasting here in case anyone wants to see.
In this example though, they are only using one type of reusable cell class. This is from my own project but it's essentially what the tutorial presents. "videoCellEntry" is a custom cell that I have created with nib file videoEntryCell.xib, "videoEntry" is the class for each cell that I am configuring, and "videos" is an array of videoEntry.
I'm assuming to use multiple nib files, I can put some conditions to choose which nib file I want and then call a different loadNibNamed: portion like so:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"videoCellId";
videoEntryCell *cell =
(videoEntryCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if(<condition 1>) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"videoEntryCell" owner:self options:nil];
}
if(<condition 2>) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"videoEntryCell2" owner:self options:nil];
}
cell = (videoEntryCell *)[nib objectAtIndex:0];
}
// Configure the cell.
videoEntry *vid = [videos objectAtIndex:indexPath.row];
[cell configureForVideoEntry:vid];
return cell;
}
But can the tableViewController handle multiple cell nib files? Or is there a better way? And wouldn't each cell require a different CellIdentifier depending on its nib file?
Yes, you can use multiple Nib-Files and multiple CellIdentifiers in the TableViewController. Just put your condition before the CellIdentifier. That's the way I've done it.
Some example inside the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath method:
First define the variable cell:
UITableViewCell *cell;
Now the if-Block:
if(<condition 1>) {
static NSString *CellIdentifier = #"VideoCell1";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"videoEntryCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (UITableViewCell *) currentObject;
break;
}
}
}
// customize your cell here
}
if(<condition 2>) {
static NSString *CellIdentifier = #"VideoCell2";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"videoEntryCell2" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (UITableViewCell *) currentObject;
break;
}
}
}
// customize your cell here
}
and finally:
return cell;
You can repeat the if-Block as often as you want. It's only important that you return a cell, which is not nil.