UITableViewCell loading issues - iphone

i have a UITableViewCell loading 10 images its loads the first 6, then its saying (cell != nil) so it doesnt load the remaining images, but if i remove the "if(cell==nil)" it loads all the images
am i missing something?
thanks
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
AsyncImageView *asyncImageView = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
int section = indexPath.section;
NSMutableArray *sectionItems = [sections objectAtIndex:section];
if (cell == nil) {
NSLog(#"CELL == NIL %#", cell);
int n = ([sectionItems count]) ;
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
int i=0,f=0;
while (i < n)
{
int yy = 4 +f*74;
for(int j=0; j<3;j++){
Item *item = [sectionItems objectAtIndex:i];
int buttonX = 10;
if (i == 0) {
buttonX = 10;
}else {
buttonX = 203
}
CGRect frame;
frame.origin.x = buttonX;
frame.origin.y = yy;
frame.size.width = 107;
frame.size.height = 107;
asyncImageView = [[[AsyncImageView alloc] initWithFrame:frame] autorelease];
asyncImageView.tag = ASYNC_IMAGE_TAG;
NSString *urlString = item.image;
NSURL *url = [NSURL URLWithString:urlString];
[asyncImageView loadImageFromURL:url];
[cell.contentView addSubview:asyncImageView];
i++;
}
f = f+1;
}
}
else {
NSLog(#"cell != nill");
asyncImageView = (AsyncImageView *) [cell.contentView viewWithTag:ASYNC_IMAGE_TAG];
}
return cell;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#"CELL == NIL %#", cell);
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
CGRect frame = CGRectMake(0,20,30,40);
UIImageView *img = [[[UIImageView alloc] initWithFrame:frame];
img.tag = IMAGE_TAG;
[cell.contentView addSubview:img];
[img release];
}int row = indexPath.row;
UIImageView *myImage = = (UIImageView *) [cell.contentView viewWithTag:IMAGE_TAG];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"%d" , row]];
[myImage setImage:image];
return cell;}
UITableViewCells are cached to save memory and allocations costs (hence performance). You only create a cell once and after that you set it contents based on your index path. I've simply created an imageview in the cell and later on set an image depending on the indexpath in that cell (1.png , 2.png etc).
Read the documentation here.

UITableViewCells are re-used to save time and memory, typically, and you're using this paradigm I see. You need to implement the cell != nil case and set up the cell to display the contents based on the indexPath. The cell was previously used to display other content so you need to adjust all indexPath-dependent views in the non-nil cell.

Related

Insert NSarray items into specified UItableviewcell

I have a UItableview with 10 cells.I need to display a message from an NSarray into this tableview.The array contains 3 items and needs to be displayed in the any of the 3 cells .
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomTableView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ CustomTableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSString *order = [orderArray objectAtIndex:indexPath.row];
NSString *sdate = [dateArray objectAtIndex:indexPath.row];
//Label positions
UILabel *ordernum = [[UILabel alloc] initWithFrame:CGRectMake(5,28,95,21)];
UILabel *date = [[UILabel alloc] initWithFrame:CGRectMake(100,28,80,21)];
UILabel *orderStatusMessage = [[UILabel alloc] initWithFrame:CGRectMake(200,28,80,21)];
NSString *ordertypeName = [ordertypeArray objectAtIndex:indexPath.row];
ordernum.textColor = [UIColor blackColor];
[cell.contentView addSubview:ordernum];
date.textColor = [UIColor blackColor];
[cell.contentView addSubview:date];
orderStatusMessage.textColor = [UIColor blackColor];
if ([ordertypeName isEqualToString:#"saved"]) {
ordernum.text = [NSString stringWithString:#"Saved Order"];
date.text = [NSString stringWithFormat:#"%#",sdate];
}
else{
if (![order isEqualToString:#""]) {
for (int i=0; i<[reversed count]; i++)
{
stat=[reversed objectAtIndex:i];
}
}
ordernum.text = [NSString stringWithFormat:#"%#",order];
date.text = [NSString stringWithFormat:#"%#",sdate];
orderStatusMessage.text =[NSString stringWithFormat:#"%#",stat];
[cell.contentView addSubview:orderStatusMessage];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
// }
return cell;
}
With the above code I am getting only the last item from reversed array.Please help
To start, your for loop is bogus
if (![order isEqualToString:#""]) {
for (int i=0; i<[reversed count]; i++)
{
stat=[reversed objectAtIndex:i];
}
}
Why are you looping and overwriting the reference to stat with each iteration? That's clearly not your intent.
Second, I would advise refactoring, since this code snippit is extremely confusing and it is very close to impossible to understand the intent. Please repost after renaming variables and cleaning the code a bit.

Why aren't my NSMutableArray objects being added to the UITableView straight away?

I have an NSMutableArray with objects within it and I use the following code to add/remove objects from the UITableView. It works but only after closing and relaunching the app, not straight away. Why is this? Here is my code (maincelltext is the title in the cell and subtitlecelltext is the subtitle):
maincelltext = [[NSMutableArray alloc] init];
subtitlecelltext = [[NSMutableArray alloc] init];
[maincelltext addObject:#"TITLE"];
[subtitlecelltext addObject:#"SUBTITLE TEST"];
EDIT: Here is my UITableView code:
- (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];
}
cell.textLabel.font = [UIFont fontWithName:#"HelveticaBold" size:16.0];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.numberOfLines = 1;
// Configure the cell.
UIImage *cellImage = [UIImage imageNamed:#"warning.png"];
CGRect cropRect = CGRectMake(0.0, 0.0, 12.0, 12.0);
CGImageRef croppedImage = CGImageCreateWithImageInRect([cellImage CGImage], CGRectMake(0.0f,0.0f,cellImage.size.width,cellImage.size.height));
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect];
[myImageView setImage:[UIImage imageWithCGImage:croppedImage]];
CGImageRelease(croppedImage);
[self.view addSubview:myImageView];
cell.imageView.image = cellImage;
NSString *subjectString = [maincelltext objectAtIndex: [indexPath row]];
cell.textLabel.text = subjectString;
NSString *subtitle = [subtitlecelltext objectAtIndex: [indexPath row]];
cell.detailTextLabel.text = subtitle;
if(maincelltext.count == 0){
NSString *notabledata = [NSString stringWithFormat:#"No Dates Set"];
[maincelltext addObject:notabledata];
}
return cell;
}
Thanks!
you need to tell the UITableView that it needs to insert the rows. First determine the index in the array the new object(s) are now at, then tell the UITableView to insert the rows like this:
NSIndexPath *indexPathOfNewObject=[NSIndexPath indexPathForRow: newObjectIndex section:0];
NSArray *indexPathArray=[NSArray arrayWithObject: indexPathOfNewObject];
[self.tableView beginUpdates];
// Note that UITableViewRowAnimationAutomatic is for iOS5 only.
[self.tableView insertRowsAtIndexPaths: indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
by using this method, you will get the cool animation effect on the tableView.
Alternatively, you could just call [self.tableView reloadData] and it will just reload all the data.
Good luck!
Just do a [myTableView reloadData]; after you did an [myArray addObject:myObject]; and you should be fine.

Empty Table with no data display

I having problem with my table. After loading everything. My data is being loaded and string into name and loadscore. However it will not show in the table. Please help.
static const NSInteger kNameLabelTag = 1337;
static const NSInteger kScoreLabelTag = 5555;
- (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];
UILabel *nameLabel = [[UILabel new] autorelease];
[nameLabel setTag:kNameLabelTag];
[cell.contentView addSubview:nameLabel];
UILabel *highscoreLabel = [[UILabel new] autorelease];
[highscoreLabel setTag:kScoreLabelTag];
[cell.contentView addSubview:highscoreLabel];
}
// Configure the cell.
//cell.textLabel.text = [loadhighScore objectAtIndex:indexPath.row];
NSDictionary *score = [self.loadhighScore objectAtIndex:indexPath.row];
NSString *name = [score objectForKey:#"ScoreName"];
NSString *loadscore = [score objectForKey:#"HighScore"];
[(UILabel *)[cell.contentView viewWithTag:kNameLabelTag] setText:name];
[(UILabel *)[cell.contentView viewWithTag:kScoreLabelTag] setText:loadscore];
return cell;
}
Do you have the tableview delegate and datasource connected to your class?
if you are loading in this data after the view has appeared on screen check that you have reloaded the tables data source [yourTableView reloadData];

UITableview dequeueReusableCellWithIdentifier false result

I'd done following code, on which I get repeat result from 10 index. (indexPath.row)
my data is in Dictionary
What could be the reason?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger index = indexPath.row;
NSLog(#"Current cell index : %d",index);
static NSString *CellIdentifier = #"CellIndetifier";
BOOL isCellSelected = NO;
//ListViewCell *cell = (ListViewCell *)[table dequeueReusableCellWithIdentifier:CellIdentifier];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease];
NSString * text = nil;
if(isMultipleSelect){
text = [dicData objectForKey:[sortedData objectAtIndex:index]];
if([sIndexes containsObject:[sortedData objectAtIndex:index]]){
isCellSelected = YES;
}
}
cell.textLabel.text = text;
}
return cell;
}
You get wrong result because you setup cell's text only when it is created, while the same cell can be used for several different rows. You need to move text-setting code outside of the creating cell block:
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease];
}
NSString * text = nil;
if(isMultipleSelect){
text = [dicData objectForKey:[sortedData objectAtIndex:index]];
if([sIndexes containsObject:[sortedData objectAtIndex:index]]){
isCellSelected = YES;
}
}
cell.textLabel.text = text;
You need to refactor the code like this:
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease];
}
NSString * text = nil;
if(isMultipleSelect){
text = [dicData objectForKey:[sortedData objectAtIndex:index]];
if([sIndexes containsObject:[sortedData objectAtIndex:index]]){
isCellSelected = YES;
}
}
cell.textLabel.text = text;
The if (cell == nil) path is to allocate a new cell if dequeueReusableCellWithIdentifier: didn't return anything. The rest of the code needs to be same for both cases: set up the cell.

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 {