UITableView and custom cells - objective-c

i have a tableView with a custom cell, my first cell is different with the others cells. ( it contain a small image and a label). I am doing like this :
#
pragma mark - tableView data source methods
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SuggestionCarteCell *cell = (SuggestionCarteCell *)[tableView dequeueReusableCellWithIdentifier:#"carteCell"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SuggestionCarteCell" owner:nil options:nil] ;
cell = [nib objectAtIndex:0];
}
if (indexPath.row == 0) {
// crer l'image
NSLog(#" je suis dans row 0");
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(21, 9, 13, 26)];
imageView.image = [UIImage imageNamed:#"iconeLocation"];
[cell addSubview:imageView];
cell.labelRegion.frame = CGRectMake(21+13+10, cell.labelRegion.frame.origin.y,cell.labelRegion.frame.size.width , cell.labelRegion.frame.size.height);
cell.labelRegion.text = [NSString stringWithFormat:#"A proximité,"];
}
else{
Region *region = [Region new];
region =(Region *) [arrayRegion objectAtIndex:indexPath.row];
cell.labelRegion.text =region.nomRegion;
}
return cell;
}
in the first time it's good, but when i scroll my first cell ( the small image added) appear in the other rows. What i am doing wrong ? thanks

You have two types of cell but only one reuse identifier.
Two options:
Make sure that when your cell is reused it resets its state to a default, empty state. There's a prepareForReuse method for this purpose
Define two different cells, each with their own, unique reuse identifier. That way your first, different cell will not get reused later on

Try like this
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SuggestionCarteCell *cell = (SuggestionCarteCell *)[tableView dequeueReusableCellWithIdentifier:#"carteCell"];
UIImageView *imageView;
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SuggestionCarteCell" owner:nil options:nil] ;
cell = [nib objectAtIndex:0];
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(21, 9, 13, 26)];
imageView.tag = 100;
[cell addSubview:imageView];
} else {
imageView = [cell viewWithTag:100];
}
if (indexPath.row == 0) {
// crer l'image
NSLog(#" je suis dans row 0");
imageView.image = [UIImage imageNamed:#"iconeLocation"];
cell.labelRegion.frame = CGRectMake(21+13+10, cell.labelRegion.frame.origin.y,cell.labelRegion.frame.size.width , cell.labelRegion.frame.size.height);
cell.labelRegion.text = [NSString stringWithFormat:#"A proximité,"];
}
else{
Region *region = [Region new];
region =(Region *) [arrayRegion objectAtIndex:indexPath.row];
cell.labelRegion.text =region.nomRegion;
imageView.image = nil;
}
return cell;
}
This way, the cells are reused, the image view is created only once, then you get its reference by its tag, but in one case you set its image, in the other case, you set it to nil.

Related

iOS:Selection and saving data or print the data when row is selected in table view

Iam using the did select method to select the data .so data in a particular row is selecting and aim getting the check mark also now my problem is when the check mark is on state the data should be printed in console and if its off the data should be removed from the array. my code is as below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(#"array is %#",array);
static NSString *CustomCellID = #"cell";
contactcellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellID];
if (cell == nil)
{
cell=[[contactcellTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CustomCellID];
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"contactcellTableViewCell" owner:self options:nil];
cell =[nib objectAtIndex:0];
}
cell.Firstnamelbl.text = [[array objectAtIndex:indexPath.row] objectForKey:#"first_name"];
cell.Lastnamelbl.text=[[array objectAtIndex:indexPath.row]objectForKey:#"last_name"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[array removeAllObjects];
contactcellTableViewCell *cell = (contactcellTableViewCell *) [tableView cellForRowAtIndexPath:indexPath];
static NSString *CustomCellID = #"cell";
if (cell == nil)
{
cell=[[contactcellTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CustomCellID];
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:#"contactcellTableViewCell" owner:self options:nil];
cell =[nib objectAtIndex:0];
}
if (cell.m_checkImageView.image == [UIImage imageNamed:#"Selected.png"])
cell.m_checkImageView.image = [UIImage imageNamed:#"Unselected.png"];
else
cell.m_checkImageView.image = [UIImage imageNamed:#"Selected.png"];
}
I had two labels in cell so that when the particular row is selected the data should be print on the console
i got the answer please find below
if (cell.m_checkImageView.image == [UIImage imageNamed:#"Selected.png"])
{
cell.m_checkImageView.image = [UIImage imageNamed:#"Unselected.png"];
}
else
{
cell.m_checkImageView.image = [UIImage imageNamed:#"Selected.png"];
NSLog(#"string is %#",string);
}
}
now we will see the data when row is selected

Bug in UITableViewController in Storyboard

I'm creating a UITableViewController to list names of people and a star next to their name to indicating favorite people like so
The stars light up when touched, indicting a favorite, the row number of that cell goes into an NSMutableArray which is called in this method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
When I tap a cell and add the index to the array, everything works, until I scroll down, and more stars are filled. They are in random, I believe, a few popup every time I scroll up then down, and look like this, faded stars...
This is the full star
Somehow the stars that shouldn't be filled are faded.
I cannot pin point where the stars are getting switched to on. The log only shows setting the star to on when I scroll to the particular cell.
My problem is that stars are switched on when they should not be, my array is good, I've checked that multiple times, it has to be the UITableView.
This is my code,
I only have two images of that star, one filled and one empty, and the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// NSLog(#"%i",[[cell.contentView subviews] count]);
// NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[cell.contentView subviews]];
[tableView reloadData];
UIImageView *star = cell.star;
NSLog(#"Star Tag: %i",star.tag);
if (star.tag == kStarEmpty) {
[[Global sharedGlobal].favTeachers addObject:[NSString stringWithFormat:#"%i",cell.identifierTag]];
NSLog(#"Added: %i",cell.identifierTag);
// NSLog(#"setting star image: 1");
[star setImage:[UIImage imageNamed:#"star.png"]];
[star setTag:kStarFilled];
} else if (star.tag == kStarFilled) {
[[Global sharedGlobal].favTeachers removeObject:[NSString stringWithFormat:#"%i",cell.identifierTag]];
NSLog(#"Removed: %i",cell.identifierTag);
// NSLog(#"setting star image: 2");
[star setImage:[UIImage imageNamed:#"star_empty.png"]];
[star setTag:kStarEmpty];
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *teacher = [[Global sharedGlobal].teachers objectAtIndex:indexPath.row];
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
UIImageView *starView = [[UIImageView alloc] init];
starView.image = [UIImage imageNamed:#"star_empty.png"];
starView.frame = CGRectMake(720, 2, 29, 29); //748,22
[starView setTag:kStarEmpty];
cell.star = starView;
[cell addSubview:starView];
cell.identifierTag = indexPath.row;
NSLog(#"This cell's identifier tag: %i",cell.identifierTag);
cell.textLabel.text = [[Global sharedGlobal].teachers objectAtIndex:indexPath.row];
for (int i = 0; i < [[Global sharedGlobal].favTeachers count]; i++) {
int favTeacherTag = [[[Global sharedGlobal].favTeachers objectAtIndex:i] intValue];
NSLog(#"%i",i);
NSLog(#"Fav Teacher Tag: %i",favTeacherTag);
if (cell.identifierTag == favTeacherTag) {
NSLog(#"found fav teacher: %i",cell.identifierTag);
NSLog(#"------------------------------------------");
// NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[cell.contentView subviews]];
NSLog(#"setting star image");
[starView setImage:[UIImage imageNamed:#"star.png"]];
NSLog(#"Previous star tag: %i",starView.tag);
[starView setTag:kStarFilled];
break;
}
NSLog(#"--------------------------------");
}
return cell;
}
EXTRA INFO:
I have a custom class for the cells, which adds the cell.identifierTag as an int.
I am using Storyboard
I use static cells in Storyboard
Thank you! If you need any more information please comment and ask.
You need to make sure to set the UIImage to nil in the else block here:
if (cell.identifierTag == favTeacherTag) {
//your existing code
} else {
[starView setImage:nil];
};
This is because the cells are reused, and you may be getting a cell that had previously had the star image added.
call [tableView reloadData] at end of (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method
After two days of this I finally figured it out. :D
I added this code, because I was using the same UIImageView sometimes and kept adding image views every time this - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath was called.
UIImageView *starView = [[UIImageView alloc] init];
if (cell.star == nil) {
starView.image = [UIImage imageNamed:#"star_empty.png"];
starView.frame = CGRectMake(720, 2, 29, 29); //748,22
[starView setTag:kStarEmpty];
cell.star = starView;
[cell addSubview:starView];
} else if (cell.star != nil) {
starView = cell.star;
}

How to delegate an open source tableview badge to UITableview

I know similar questions have been asked few times about how to add badge to tableviewcell, but I could not make it working
Basically what I want is to show user a simple notification either a red number at the right part of the table view cell or a rectangle or like native email app.
So I have tried both of this two source code TDbadgcell and DDbadgecell
Now the problem is I can not delegate them, I have to import their .h classes and call either one of the below functions in my table view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TDBadgedCell *cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
or
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
DDBadgeViewCell *cell = (DDBadgeViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DDBadgeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
But when I do that my tableView didSelectRowAtIndexPath: and (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender methods are not working, I can click the rows but they stay higlihted blue nothing happens also my arrow at the right side of the table is dissappears.
So how can I achieve to add a badge to table view cell row either with above source codes or any other methods?
EDIT:::
After putting NSLOG I can see that did select row is called but perform segue still does not work. Without adding any of the above code it works perfect.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = #"MeetingCell";
static NSString *CellIdentifier = #"Cell";
DDBadgeViewCell *cell = (DDBadgeViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DDBadgeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *description =#":";
NSString *name =#"";
NSString *fileStatus=#"";
name = [[self agenda] getFileNameWithSection:[indexPath section] Row:[indexPath row]];
description = [[self agenda] getFileDescriptionWithSection:[indexPath section] Row:[indexPath row]];
fileStatus = [[self agenda] getFileStatusWithFileName:name];
NSString * cellLabel = [NSString stringWithFormat:#" %# : %#",description,name];
//alloc row images
UIImage *docImage = [UIImage imageNamed:#"ICON - Word#2x.png"];
UIImage *xlsImage = [UIImage imageNamed:#"ICON - Excel#2x.png"];
// UIImage *picImage = [UIImage imageNamed:#"ICON - Image#2x.png"];
UIImage *pdfImage = [UIImage imageNamed:#"pdf icon#2x copy.png"];
UIImage *pptImage = [UIImage imageNamed:#"ICON - PPT#2x.png"];
//Determine what status to display for a file
//No need to that since wee use push notification
if ([fileStatus isEqualToString:#"new"]){
cellLabel = [NSString stringWithFormat:#"%# (%#)",cellLabel,#"New"];
cell.badgeText = [NSString stringWithFormat:#"Update"];
cell.badgeColor = [UIColor orangeColor];
}else if ([fileStatus isEqualToString:#"outdated"]){
cellLabel = [NSString stringWithFormat:#"%# (%#)",cellLabel,#"Outdated"];
cell.badgeText = [NSString stringWithFormat:#"Update"];
cell.badgeColor = [UIColor orangeColor];
}else if ([fileStatus isEqualToString:#"updated"]){
cellLabel = [NSString stringWithFormat:#"%# (%#)",cellLabel,#"Latest"];
}
UIFont *font1 = [UIFont fontWithName:#"Century Gothic" size:15.0f];
cell.textLabel.font=font1;
//if there is no file user can not tocuh the row
if ([name length]==0) {
cell.userInteractionEnabled = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = description;
}else{
//set cell title
cell.textLabel.text = cellLabel;
}
//set row images
if ([name rangeOfString:#"docx"].location != NSNotFound) {
cell.imageView.image= docImage;
}else if ([name rangeOfString:#"xlsx"].location != NSNotFound){
cell.imageView.image= xlsImage;
}
else if ([name rangeOfString:#"pdf"].location != NSNotFound){
cell.imageView.image= pdfImage;
}
else if ([name rangeOfString:#"ppt"].location != NSNotFound){
cell.imageView.image= pptImage;
}
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rounded corner box center#2x.png"]];
// At end of function, right before return cell
cell.textLabel.backgroundColor = [UIColor clearColor];
NSLog(#"%#",cell.textLabel.text);
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];.0
*/
NSLog(#"didselect row is called %d",indexPath.row);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[DBSession sharedSession] isLinked]) {
if([[segue identifier] isEqualToString:#"pushDocumentView"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSInteger section =[indexPath section];
NSInteger row = [indexPath row];
NSString *fileName = [[self agenda] getFileNameWithSection:section Row:row];
NSDictionary * agendaDic = [[[self agenda]revision] objectForKey:fileName];
NSString *fileStatus =[agendaDic objectForKey:#"status"];
DocumentViewController *docViewController = [segue destinationViewController];
//This will display on the Document Viewer
docViewController.fileName=fileName;
//This will determine remote or local copy display
docViewController.fileStatus=fileStatus;
}
}else {
[self displayError];
[self setWorking:NO];
}
}
Just called a perfromseguewith Identifier
because this is called - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender also when you call [self performSegueWithIdentifier:#"yoursegue" sender:self];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];.0
*/
[self performSegueWithIdentifier:#"yoursegue" sender:self];
NSLog(#"didselect row is called %d",indexPath.row);
}

Making all the text show in a UITableView Cell

I am loading an array into a UITableView and the components of the array are long. What I want to do is customize the cells so that thier lenght will adjust to fit the text but the width will stay the same. Right now what is happening is when the text gets too long it will just write off the cell and you will see this.
I would like to see the text hit the end of the cell and then start a new line.
My code right now looks like this.
#implementation CRHCommentSection
#synthesize observationTable;
NSArray *myArray;
- (void)viewDidLoad
{
myArray = [CRHViewControllerScript theArray];
NSLog(#"%#", myArray);
//NSArray* paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:1]];
//[observationTable insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
[observationTable reloadData];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
NSLog(#" in method 1");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#" in method 2");
// Return the number of rows in the section.
return [myArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#" in method 3");
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [myArray objectAtIndex:indexPath.row];
//cell.textLabel.text = #"Label";
return cell;
}
I also found some code that somebody else wrote for multiple lined cells but don't know how to make it automatic based on the length of my string from the array.
static NSString *CellIdentifier = #"MyCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2
reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = #"Label';
cell.detailTextLabel.text = #"Multi-Line\nText";
cell.detailTextLabel.numberOfLines = 2;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
They said that you will also need to return a suitable height for the multi-line cell and that a height of (44.0 + (numberOfLines - 1) * 19.0) should work fine.
Does anyone have any ideas of how to do this?
Thanks.
You will need to work with following methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// this method is called for each cell and returns height
NSString * text = #"Your very long text";
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize: 14.0] forWidth:[tableView frame].size.width - 40.0 lineBreakMode:NSLineBreakByWordWrapping];
// return either default height or height to fit the text
return textSize.height < 44.0 ? 44.0 : textSize.height;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellIdentifier = #"YourTableCell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:cellIdentifier];
[[cell textLabel] setNumberOfLines:0]; // unlimited number of lines
[[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
[[cell textLabel] setFont:[UIFont systemFontOfSize: 14.0]];
}
[[cell textLabel] setText:#"Your very long text"];
return cell;
}
You want to look at the NSString function -(CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
You will have to include it in your heightForRowAtIndexPath function to determine the right heights.
Also, set the numberOfLines for your label to 0. This allows it to adjust to whatever number of lines is needed.
How about using UITextView in table view cell.
At first, you have to change the height of cell.
Next, you put the UITextView into the cell.
Off course you can do this not by IB but by code.
For me the accepted answer worked by adding the else condition.
In my case, if condition was not getting satisfied as the [tableView dequeueReusableCellWithIdentifier:#"comment"]; was always returning cell which was not nil.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger item = indexPath.item;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"comment"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:#"comment"];
[[cell textLabel] setNumberOfLines:0]; // unlimited number of lines
[[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
[[cell textLabel] setFont:[UIFont systemFontOfSize: 14.0]];
}
// added else condition
else {
[[cell textLabel] setNumberOfLines:0];
[[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
[[cell textLabel] setFont:[UIFont systemFontOfSize: 14.0]];
}
[[cell textLabel] setText:commentsText[item]];
return cell;
}

adding different UILabel to each cell in UITableView

I'm adding different text to each cell in UITableView. However when I do that, test is displayed in every cell except for the first one..So if theres an array with 9 numbers between 1 to 9, 1 is displayed in the second cell, 2 is displayed in the third cell and respectively. There's nothing shown in the first cell fromHeres the codes
// Customize the appearance of table view cells.
- (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];
cell = [self getCellContentView:CellIdentifier];
}
if (indexPath.row == 0) {
NSLog(#"hi");
}
//add another textfield
NSString *path = [[NSBundle mainBundle] pathForResource:#"Rankvalue" ofType:#"plist"];
NSMutableArray* rank = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSString *rankValue = [rank objectAtIndex:indexPath.row];
UILabel *rankLabel = (UILabel *)[cell viewWithTag:1];
rankLabel.text = rankValue;
[rankLabel setFont:[UIFont fontWithName:#"austin power" size:[#"40" intValue]]];
CGRect labelFrame = CGRectMake(220,70,50,40.0);
[rankLabel setFrame:labelFrame];
// Configure the cell(thumbnail).
cell.textLabel.text = [self.stars objectAtIndex:indexPath.row];
NSString *path2 = [[NSBundle mainBundle] pathForResource:#"Filename" ofType:#"plist"];
self.filename = [[NSMutableArray alloc] initWithContentsOfFile:path2];
cell.imageView.image = [UIImage imageNamed:[self.filename objectAtIndex:indexPath.row]];
//transparent cell
cell.backgroundColor = [UIColor clearColor];
[rankLabel release];
[rankValue release];
return cell;
}
And this is the code for the subview of the cell
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
CGRect CellFrame = CGRectMake(0, 0, 320, 65);
CGRect Label1Frame = CGRectMake(17,5,250,18);
UILabel *lblTemp;
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
[lblTemp setFont:[UIFont fontWithName:#"Arial-Bold" size:15]];
lblTemp.tag = 1;
lblTemp.backgroundColor=[UIColor clearColor];
lblTemp.numberOfLines=0;
[cell.contentView addSubview:lblTemp];
return cell;
}
initWithFrame:reuseIdentifier: has been deprecated for initWithStyle:reuseIdentifier:
You should create one of the standard styles.
Set the values you want (font, etc.) on cell creation, not on refresh. Frame of text field is the same. And background color.
You really don't want to reload that plist (oh, there's two of them!) every time you refresh a cell! Load it once in another method and cache it as an ivar.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// create and set up all the layout attributes of the cell
// it should be auto released
// which should include a call like the following, or loading a cell from a nib
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
// reuseIdentifier:CellIdentifier];
// [cell autorelease];
}
// the only thing that should happen here is setting the data values
// into the cell!!!!!! All these values should be loaded once and
// kept as "model state" by the controller. Do not create an image
// each time through, do not load an entire plist to access one item
// each time through
rankLabel.text = rankValue;
cell.textLabel.text = [self.stars objectAtIndex:indexPath.row];
cell.imageView.image = [self imageAtRow:indexPath.row];
}
If the image your displaying is different for each cell, using imageNamed: is not appropriate. If there's 2 or 3 images that indicate type of cell that will each be used a lot, imageNamed: is likely preferable. imageWithContentsOfFile: is your other option, you'll need to build the full path
Try switching this:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell = [self getCellContentView:CellIdentifier];
}
for this:
UITableViewCell *cell=[self getCellContentView:CellIdentifier];
Not saying it will work, but just give it a shot. Also, do this inside your getCellContentView:
[lblTemp release]
Or you will have a leak.