UITableViewCell Repeating images - iphone

I am trying to add a favourites images in the table view cell. I am using this code
static NSString *CellIdentifier = #"memberCell";
MemberCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *item = self.searchList[indexPath.row];
cell.memberFullName.text = [item[#"full_name"] uppercaseString] ;
cell.memberPosition.text = [item[#"title"] length] < 1 ? [NSString stringWithFormat:#"%#", item[#"districts"]] : [NSString stringWithFormat:#"%#, %#", item[#"title"], item[#"districts"]];
cell.memberRoom.text = [NSString stringWithFormat:#"Rm %#", item[#"room_number"] ];
[cell.memberImage setImageWithURL:[NSURL URLWithString:item[#"image_thumb"]]
placeholderImage:[UIImage imageNamed:#"placeholder"]];
if(![item[#"fid"] isEqualToString:#"0"]) {
[cell.memberFavoriteImage setImage:[UIImage imageNamed:#"favorite"]];
}
The problem i am having is the favourite images are showing on multiple cells even though the condition if(![item[#"fid"] isEqualToString:#"0"]) is passed only for 1 cell, when i do a NSLog, the condition is passed once only but the images are displayed is a systematic order (that is each 9th row), when i scroll all the way down and come back the order completely changes and images are shown in different rows. I am not sure what is happening, please help.

The dequeue instruction potentially returns an existing instance of cell
try this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"%d_%d",indexPath.section,indexPath.row];
MemberCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MemberCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *item = self.searchList[indexPath.row];
cell.memberFullName.text = [item[#"full_name"] uppercaseString] ;
cell.memberPosition.text = [item[#"title"] length] < 1 ? [NSString stringWithFormat:#"%#", item[#"districts"]] : [NSString stringWithFormat:#"%#, %#", item[#"title"], item[#"districts"]];
cell.memberRoom.text = [NSString stringWithFormat:#"Rm %#", item[#"room_number"] ];
[cell.memberImage setImageWithURL:[NSURL URLWithString:item[#"image_thumb"]]placeholderImage:[UIImage imageNamed:#"placeholder"]];
if(![item[#"fid"] isEqualToString:#"0"]) {
[cell.memberFavoriteImage setImage:[UIImage imageNamed:#"favorite"]];
} else {
[cell.memberFavoriteImage setImage:nil];
}
return cell;
}

NSString *CellIdentifier = [NSString stringWithFormat:#"%d_%d",indexPath.section,indexPath.row];
MemberCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MemberCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *item = self.searchList[indexPath.row];
cell.memberFullName.text = [item[#"full_name"] uppercaseString] ;
cell.memberPosition.text = [item[#"title"] length] < 1 ? [NSString stringWithFormat:#"%#", item[#"districts"]] : [NSString stringWithFormat:#"%#, %#", item[#"title"], item[#"districts"]];
cell.memberRoom.text = [NSString stringWithFormat:#"Rm %#", item[#"room_number"] ];
[cell.memberImage setImageWithURL:[NSURL URLWithString:item[#"image_thumb"]]placeholderImage:[UIImage imageNamed:#"placeholder"]];
if(![item[#"fid"] isEqualToString:#"0"]) {
[cell.memberFavoriteImage setImage:[UIImage imageNamed:#"favorite"]];
}
return cell;

Related

using of two different type of custom cell in same table view?

thanks in advance.
in my app, i have a tableview, in which i have to use two different style of custom cell, i made two custom cell, and in tableView cellForRowAtIndexPath method i used two identifier for cell, even i tried for two section. but its not working. it is giving me "EXE BAD Excess" or some time other kind of error. below is my code.
Error : thread1_EXE_BAD_Access(code = 2 ,address 0 x 0)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
//CatIdentifier
static NSString *CellIdentiFier = #"CatIdentifier";
static NSString *Cell1IdentiFier = #"CatIdentifier1";
if (indexPath.section == 0)
{
CommitteCell *cell = ( CommitteCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentiFier];
if(cell == nil)
{
cell = ( CommitteCell *)[[[NSBundle mainBundle] loadNibNamed:#"CommitteeCell" owner:self options:nil] objectAtIndex:0];
}
if (indicator == 1)
{
cell.lblName.text = str;
}
else
{
cell.lblName.text = [arrayName objectAtIndex:indexPath.row];
cell.lblPost.text = [arrayPost objectAtIndex:indexPath.row];
cell.picimg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[arrayimage objectAtIndex:indexPath.row]]]];
}
cell.backgroundView = [[UIImageView alloc]init];
UIImage *img = [UIImage imageNamed:#"link-bg 2.png"];
((UIImageView *)cell.backgroundView).image = img;
return cell;
}
else
{
Committee2Cell *cell1 = (Committee2Cell *)[tableView dequeueReusableCellWithIdentifier:Cell1IdentiFier];
if(cell1 == nil)
{
cell1 = (Committee2Cell *)[[[NSBundle mainBundle] loadNibNamed:#"Committee2Cell" owner:self options:nil] objectAtIndex:0];
}
cell1.lblPost1.text = strPost;
cell1.txtName.text = strName;
cell1.backgroundView = [[UIImageView alloc]init];
UIImage *img = [UIImage imageNamed:#"link-bg 2.png"];
((UIImageView *)cell1.backgroundView).image = img;
return cell1;
}
}
section in tableview and rows in section method are as below.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
switch (section)
{
case 0:
return [arrayName count]-1;
break;
case 1:
return 1;
break;
default:
break;
}
return 0;
}
please if anyone can fine that where is my mistake . thanks again.
data of array and label is as below.
-(void)NewsParser:(NSMutableDictionary *)dic
{
NSLog(#"dic = %#",dic);
arrayName = [[NSMutableArray alloc]init];
arrayPost = [[NSMutableArray alloc]init];
arrayimage= [[NSMutableArray alloc]init];
strPost = [[NSString alloc]init];
strName = [[NSString alloc]init];
strPost = [[dic valueForKey:#"post"]objectAtIndex:8];
strName = [[dic valueForKey:#"name"]objectAtIndex:8];
NSLog(#"Name = %#",strName);
NSLog(#"Post = %#",strPost);
for(int i=0;i<[dic count]-1;i++)
{
[arrayName addObject:[[dic valueForKey:#"name"]objectAtIndex:i]];
[arrayPost addObject:[[dic valueForKey:#"post"]objectAtIndex:i]];
[arrayimage addObject:[[dic valueForKey:#"pic"]objectAtIndex:i]];
}
NSLog(#"array = %#",arrayName);
NSLog(#"array = %#",arrayPost);
NSLog(#"array = %#",arrayimage);
[table1 reloadData];
}
I think a cleaner approach would be to use a container view with two different kind of cells and then selectively show/hide the view relevant for that cell. This would be easier to code and maintain but might consume a little more memory.
You R making reUsable identifier as only once . Do something like this :
-(UITableViewCell *)tableView : (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* identifier;
if(indexPath.section == 0)
identifier = #"0";
else
identifier = #"1";
self.tableView.dataSource = self;
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
if( cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] ;
if (indexPath.section == 0)
{
if(cell == nil)
{
cell = ( CommitteCell *)[[[NSBundle mainBundle] loadNibNamed:#"CommitteeCell" owner:self options:nil] objectAtIndex:0];
}
if (indicator == 1)
{
cell.lblName.text = str;
}
else
{
cell.lblName.text = [arrayName objectAtIndex:indexPath.row];
cell.lblPost.text = [arrayPost objectAtIndex:indexPath.row];
cell.picimg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[arrayimage objectAtIndex:indexPath.row]]]];
}
cell.backgroundView = [[UIImageView alloc]init];
UIImage *img = [UIImage imageNamed:#"link-bg 2.png"];
((UIImageView *)cell.backgroundView).image = img;
return cell;
}
Use like follow its work in my code smoothly , if you need more help let me know :
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(YOUR CONDITION HERE)
ShareActionViewCell *shareCell;
NSString *ShareCellId = [NSString stringWithFormat:#"ShareCell%d",indexPath.row];
shareCell = (ShareActionViewCell *)[tableView dequeueReusableCellWithIdentifier:ShareCellId];
if(!shareCell) {
shareCell = [[ShareActionViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ShareCellId];
}
shareCell.selectionStyle = UITableViewCellSelectionStyleNone;
shareCell.ShareTitle.text = [NSString stringWithFormat:#"%#",[tbldata objectAtIndex:indexPath.row]];
} else {
CustCell *dataCell;
NSString *DataCellId = [NSString stringWithFormat:#"DataCell%d",indexPath.row];
dataCell = (CustCell *)[tableView dequeueReusableCellWithIdentifier:DataCellId];
if(!dataCell) {
dataCell = [[CustCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DataCellId];
}
shareCell.selectionStyle = UITableViewCellSelectionStyleNone;
shareCell.ShareTitle.text = [NSString stringWithFormat:#"%#",[tbldata objectAtIndex:indexPath.row]];
}
}
Suggested using -objectForKey for a Dictionary:
[[dic objectForKey:#"post"] objectAtIndex:8];
Make sure there is a NSArray object at name/post/ pic keyed to dic
And, in your for loop:
for(int i=0;i<[dic count]-1;i++)
{
[arrayName addObject:[[dic valueForKey:#"name"] objectAtIndex: i]];
[arrayPost addObject:[[dic valueForKey:#"post"]objectAtIndex:i]];
[arrayimage addObject:[[dic valueForKey:#"pic"]objectAtIndex:i]];
}
are you sure [dic count] <= [dic objectForKey:#"name"]?
add a nil to array will be crashed.
4.Where did you call the method -(void)NewsParser:(NSMutableDictionary *)dic;, If your data array is correct, maybe the [table1 reloadData]; crashed.

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

Searching a UITableview

I am trying to do a search in a UITableview. I have implemented the UISearchDisplayDelegate, UISearchBarDelegate method at the correct way. This is how my cellForRowAtIndexPath looks like.
- (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];
}
if (tableView == self.searchDisplayController.searchResultsTableView){
Contact *contact = [self.filteredListContent objectAtIndex:indexPath.row];
NSString *text = [NSString stringWithFormat:#"%# %#",contact.name,contact.firstName];
NSLog(#"CellForRowAtIndexPath contact text is %#",text);
cell.textLabel.text = text;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}else{
NSString *alphabet = [firstIndex objectAtIndex:[indexPath section]];
//---get all states beginning with the letter---
NSPredicate *predicate =
[NSPredicate predicateWithFormat:#"SELF.name beginswith[c] %#",alphabet];
NSArray *contacts = [listContent filteredArrayUsingPredicate:predicate];
Contact *contact = [contacts objectAtIndex:indexPath.row];
NSString *text = [NSString stringWithFormat:#"%# %#",contact.name,contact.firstName];
cell.textLabel.text = text;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
return cell;
}
And this is my filterContentForSearchText method
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
for (Contact *contact in listContent)
{
NSString *searchString = [NSString stringWithFormat:#"%# %#",contact.name,contact.firstName];
NSRange range = [searchString rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound) {
[self.filteredListContent addObject:contact];
[self.searchDisplayController.searchResultsTableView reloadData];
}
}
}
The strange thing is. In my cellForRowAtIndexPath it returns me the correct data. But the tableview itselfs keeps given me the NO RESULTS label.
Any help with this?
Follow this tutorial how-to-add-search-bar-uitableview ..
Just see the method cellForRowAtIndexPath: in which how to set the search array when user searched record from UISearchBar...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"RecipeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
}
return cell;
}
Use an array to show data in table(showDataArray)
Use an array to store all input values(dataArray)
Use showDataArray to populate your table always
In the searchfield when charecter range changes call a method to filter the data using predicate from dataArray
Save the value to showDataArray
Call for table reloadData
First add UISearchBar on top of UITabelView documentation
And then Take Two NSMutableArray and add one array to another array in ViewDidLoad method such like,
self.listOfTemArray = [[NSMutableArray alloc] init]; // array no - 1
self.ItemOfMainArray = [[NSMutableArray alloc] initWithObjects:#"YorArrayList", nil]; // array no - 2
[self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray]; // add 2array to 1 array
And Write following delegate Method of UISearchBar
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText
{
NSString *name = #"";
NSString *firstLetter = #"";
if (self.listOfTemArray.count > 0)
[self.listOfTemArray removeAllObjects];
if ([searchText length] > 0)
{
for (int i = 0; i < [self.ItemOfMainArray count] ; i = i+1)
{
name = [self.ItemOfMainArray objectAtIndex:i];
if (name.length >= searchText.length)
{
firstLetter = [name substringWithRange:NSMakeRange(0, [searchText length])];
//NSLog(#"%#",firstLetter);
if( [firstLetter caseInsensitiveCompare:searchText] == NSOrderedSame )
{
// strings are equal except for possibly case
[self.listOfTemArray addObject: [self.ItemOfMainArray objectAtIndex:i]];
NSLog(#"=========> %#",self.listOfTemArray);
}
}
}
}
else
{
[self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray ];
}
[self.tblView reloadData];
}
And Write in cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Foobar"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Foobar"];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.textColor = [UIColor blackColor];
}
cell.textLabel.text = [self.listOfTemArray objectAtIndex: indexPath.row];
return cell;
}

heightForRowAtIndexPath crashes

I have the following code:
- (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath{
TestCell* cell = (ConvoreCell *) [tableView cellForRowAtIndexPath:indexPath];
CGSize textSize = [[cell text] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake([tableView frame].size.width - 20, 500)];
return 80;
}
It crashes on the cellForRowAtIndexPath, why is this? The log doesn't tell me anything. Here's my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ConvoreCell";
AsyncImageView *asyncImageView = nil;
TestCell * cell = (TestCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TestCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (ConvoreCell *) currentObject;
break;
}
}
asyncImageView = [[[AsyncImageView alloc] init] autorelease];
asyncImageView.imageview = cell.icon;
}
else {
asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG];
}
NSMutableDictionary *cellValue = [results objectAtIndex:indexPath.row];
NSString *picURL = [[cellValue objectForKey:#"user"] objectForKey:#"img"];
if ([picURL isEqualToString:#"https://secure.gravatar.com/avatar/1f043010eb1652b3fab3678167dc0487/?default=https%3A%2F%2Fconvore.com%2Fmedia%2Fimages%2Feric.png&s=80"])
picURL = #"https://test.com/media/images/eric.png";
[asyncImageView loadImageFromURL:[NSURL URLWithString:picURL]];
cell.title.text = [cellValue objectForKey:#"message"];
cell.info.text = [NSString stringWithFormat:#"%# %# days ago", [[cellValue objectForKey:#"user"] objectForKey:#"username"], [cellValue objectForKey:#"date_created"] ];
return cell;
}
All I want to do is to resize the height of the cell according to the cell.title.text....
It's wrong to call cellForRowAtIndexPath: manually. You have to store those strings separately in array and obtain them from there.
A rule of thumb: don't call framework callbacks manually.

UITableViewCell with UITableViewCellStyleValue1, adding new line to detailTextLabel at cell at bottom

on my tableview i have the last cell that is not initially visible as seen in the first image, when i scroll the list up, you can see in the second image that the price or my detailTextLabel is put on a new line not maintaining the right justification.
image 1 http://img706.imageshack.us/img706/4496/iphoneerror1.jpg
image 2 http://img706.imageshack.us/img706/6007/iphoneerror2edited.jpg
Here is the code, i can't figure out why its doing this, any direction or help would be much appreciated
- (UITableViewCell *)tableView:(UITableView *)ltableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [ltableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell.
NSUInteger indexRow = [indexPath row];
switch (indexRow) {
case 0:{
NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet];
NSString *description = [[currentData objectForKey:#"Description"] stringByTrimmingCharactersInSet:set];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
cellShift = 1;
if (![description isEqualToString:#""]) {
cell.textLabel.text = #"";
cell.detailTextLabel.text = description;
cell.detailTextLabel.numberOfLines = 2;
}
else {
cell.textLabel.text = #"";
cell.detailTextLabel.text = #"";
cell.detailTextLabel.numberOfLines = 0;
}
break;
}
default:{
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
}
NSDictionary *item = [tableData objectAtIndex:(indexRow-cellShift)];
NSString *name = [item objectForKey:#"Name"];
if ([name length] > MaxVendorsLength ) {
name = [NSString stringWithFormat:#"%# ...",[name substringToIndex:MaxVendorsLength]];
}
cell.textLabel.text = name;
cell.textLabel.minimumFontSize = 12;
NSString *priceString;
float price = [[item objectForKey:#"Price"] floatValue];
//NSLog(#"| %# | : | %# |",[item objectForKey:#"Name"], [item objectForKey:#"Price"]);
if (price != 0) {
priceString = [[NSString alloc] initWithFormat:#"$%.2f",price];
}
else {
priceString = [[NSString alloc] initWithString:#"--"];
}
cell.detailTextLabel.text = priceString;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[priceString release];
break;
}
}
cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
cell.textLabel.minimumFontSize = 14;
cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:15];
cell.detailTextLabel.minimumFontSize = 14;
return cell;
}
Let me know if i need to post anything else to get help with this ???
It's because when you scroll up the top cell is reused because all of your cells have the same cell identifier (the first line you have). You need to declare the two cell identifiers and use the appropriate one based on what row you're trying to get.
static NSString *FirstRowCellIdentifier = #"A";
static NSString *OtherRowCellIdentifier = #"B";
NSString *cellIdentifier = nil;
if ([indexPath row] == 0)
cellIdentifier = FirstRowCellIdentifier;
else
cellIdentifer = OtherRowCellIdentifier;
UITableViewCell *cell = [ltableView dequeueReusableCellWithIdentifier:cellIdentifier];
// .....
Then you can use the rest of your code as is. This just ensures that the resused cell is of the correct type.
You're using the same cell identifier for all rows but the 0th row has a different style. When scrolling up, a subtitle-styled cell might be getting re-used.
Try using a different cell identifier for the 0th row. Keep just the declaration of cell outside the switch and move the dequeueReusableCellWithIdentifier to each case.
(UITableViewCell *)tableView:(UITableView *)ltableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {