Error reloading custom cell after changing data - iphone

I have a set up tableView which works fine.
As you can see below I want to add an image to my cell when the value for the object #"sent" changes to #"yes". After this is done i call [sendTable reloadData] but nothing happens to the tableView. Only when I restart the app my image is properly shown in the cell.
So obviously reloadData isn't doing anything here.
Can anybody figure out the problem?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (tableView == sendTable) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:#"savedItems.plist"];
savedItemsArray = [NSMutableArray arrayWithContentsOfFile:writableDBPath];
NSDictionary* item = [savedItemsArray objectAtIndex:indexPath.row];
NSString *temp = [NSString stringWithFormat:#"%#,%#", [[savedItemsArray valueForKey:#"name"]objectAtIndex:indexPath.row],
[[savedItemsArray valueForKey:#"message"]objectAtIndex:indexPath.row]];
UIView *background;
cell = [self.sendTable dequeueReusableCellWithIdentifier:temp];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:temp];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
background = [[UIView alloc] initWithFrame:cell.frame];
[cell addSubview:background];
[cell sendSubviewToBack:background];
}
if ([[item objectForKey:#"sent"] isEqualToString: #"no"]) {
UILabel *personLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, cell.frame.size.width-150, 20)];
personLabel.backgroundColor = [UIColor clearColor];
personLabel.text = [NSString stringWithFormat: #"%#",[item objectForKey:#"name"]];
personLabel.font = [UIFont boldSystemFontOfSize:17];
UILabel *personLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, cell.frame.size.width-150, 20)];
personLabel2.backgroundColor = [UIColor clearColor];
personLabel2.text = [NSString stringWithFormat: #"(%#)", [item objectForKey:#"number"]];
personLabel2.font = [UIFont systemFontOfSize:15];
UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(cell.frame.size.width-125, 10, 80, 40)];
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.text = [NSString stringWithFormat: #"%#\n%#",[item objectForKey:#"date"], [item objectForKey:#"time"]];
dateLabel.textColor = [UIColor darkGrayColor];
dateLabel.textAlignment = NSTextAlignmentRight;
dateLabel.font = [UIFont systemFontOfSize:13];
dateLabel.numberOfLines = 2;
UILabel *messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, cell.frame.size.width-50, 20)];
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.text = [NSString stringWithFormat: #"%#",[item objectForKey:#"message"]];
messageLabel.textColor = [UIColor lightGrayColor];
messageLabel.font = [UIFont systemFontOfSize:15];
[background addSubview:personLabel];
[background addSubview:personLabel2];
[background addSubview:dateLabel];
[background addSubview:messageLabel];
[cell addSubview:background];
}else{
UILabel *personLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 10, cell.frame.size.width-170, 20)];
personLabel.backgroundColor = [UIColor clearColor];
personLabel.text = [NSString stringWithFormat: #"%#",[item objectForKey:#"name"]];
personLabel.font = [UIFont boldSystemFontOfSize:17];
UILabel *personLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(40, 30, cell.frame.size.width-170, 20)];
personLabel2.backgroundColor = [UIColor clearColor];
personLabel2.text = [NSString stringWithFormat: #"(%#)", [item objectForKey:#"number"]];
personLabel2.font = [UIFont systemFontOfSize:15];
UILabel *dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(cell.frame.size.width-125, 10, 80, 40)];
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.text = [NSString stringWithFormat: #"%#\n%#",[item objectForKey:#"date"], [item objectForKey:#"time"]];
dateLabel.textColor = [UIColor darkGrayColor];
dateLabel.textAlignment = NSTextAlignmentRight;
dateLabel.font = [UIFont systemFontOfSize:13];
dateLabel.numberOfLines = 2;
UILabel *messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, 60, cell.frame.size.width-80, 20)];
messageLabel.backgroundColor = [UIColor clearColor];
messageLabel.text = [NSString stringWithFormat: #"%#",[item objectForKey:#"message"]];
messageLabel.textColor = [UIColor lightGrayColor];
messageLabel.font = [UIFont systemFontOfSize:15];
UIImage *checkmarkImage = [UIImage imageNamed:#"done.png"];
UIImageView *checkmark = [[UIImageView alloc] initWithImage:checkmarkImage];
[checkmark setFrame:CGRectMake(10, 35, 20, 20)];
[background addSubview:personLabel];
[background addSubview:personLabel2];
[background addSubview:dateLabel];
[background addSubview:messageLabel];
[background addSubview:checkmark];
[cell addSubview:background];
}
}
return cell;
}
Thanks a lot!

As you are adding several UILabel and UIView to a cell,i think u should customize your own UITableViewCell.
Try to create a class subclassing UITableviewCell, and add subview in the implement.
Hope my answer is helpful

Related

Overlapping Table cells-content [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
recently I discovered a really annoying bug:
the content of two cells are mixed up in one cell
I have 16 cells data to display on scrolling the previeously existing cell data get overlapped with current data to be displayed
#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
if (flag == NO) {
return 10;
}
return [ytdRate count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 1;
}
- (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 (flag == NO) {
b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];
// NSString *stri = [title objectAtIndex:indexPath.section];
b1Lab1.text=[[sortedArray mutableArrayValueForKey:companyNam]objectAtIndex:indexPath.section];
b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab1.numberOfLines = 0;
b1Lab1.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab1];
b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
NSString *st = [NSString stringWithFormat:#"YTD %#%%",[[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section]];
b1Lab2.textAlignment=NSTextAlignmentCenter;
b1Lab2.text=st;
b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab2.numberOfLines = 0;
b1Lab2.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
NSString *tem = [[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section];
// NSArray *testItems = [tem componentsSeparatedByString:#"%"];
//
// NSDecimalNumber *value = [testItems objectAtIndex:0];
float tempr = [tem floatValue];
if(tempr <0){
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_red_80x20.png"]];
}
else{
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_green_80x20.png"]];
}
[cell addSubview:b1Lab2];
b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
NSString *st1 = [NSString stringWithFormat:#"Net Asset Value Per Share"];
b1Lab3.text=st1;
b1Lab3.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab3];
b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
b1Lab8.textAlignment = NSTextAlignmentRight;
NSString *st10 = [NSString stringWithFormat:#"%#", [[sortedArray mutableArrayValueForKey:netVal]objectAtIndex:indexPath.section]];
b1Lab8.text=st10;
b1Lab8.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab8];
b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];
NSString *st11 = [NSString stringWithFormat:#"1 YR: %#%%",[[sortedArray mutableArrayValueForKey:oneYear]objectAtIndex:indexPath.section]];
b1Lab4.text=st11;
b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab4.numberOfLines = 0;
b1Lab4.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab4];
b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];
NSString *st12 = [NSString stringWithFormat:#"3 YR: %#%%",[[sortedArray mutableArrayValueForKey:threeYear]objectAtIndex:indexPath.section]];
b1Lab5.text=st12;
b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab5.numberOfLines = 0;
b1Lab5.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab5];
b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];
NSString *st13 = [NSString stringWithFormat:#"5 YR: %#%%",[[sortedArray mutableArrayValueForKey:fiveYear]objectAtIndex:indexPath.section]];
b1Lab6.text=st13;
b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab6.numberOfLines = 0;
b1Lab6.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab6];
cell.backgroundColor = [UIColor whiteColor];
}
else if(flag == YES)
{
b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];
// NSString *stri = [title objectAtIndex:indexPath.section];
b1Lab1.text=[companyName objectAtIndex:indexPath.section];
b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab1.numberOfLines = 0;
b1Lab1.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab1];
b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
NSString *st = [NSString stringWithFormat:#"YTD %#%%",[ytdRate objectAtIndex:indexPath.section]];
b1Lab2.textAlignment=NSTextAlignmentCenter;
b1Lab2.text=st;
b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab2.numberOfLines = 0;
b1Lab2.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
NSString *tem = [ytdRate objectAtIndex:indexPath.section];
// NSArray *testItems = [tem componentsSeparatedByString:#"%"];
//
// NSDecimalNumber *value = [testItems objectAtIndex:0];
float tempr = [tem floatValue];
if(tempr <0){
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_red_80x20.png"]];
}
else{
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_green_80x20.png"]];
}
[cell addSubview:b1Lab2];
b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
NSString *st1 = [NSString stringWithFormat:#"Net Asset Value Per Share"];
b1Lab3.text=st1;
b1Lab3.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab3];
b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
b1Lab8.textAlignment = NSTextAlignmentRight;
NSString *st10 = [NSString stringWithFormat:#"%#", [netAsset objectAtIndex:indexPath.section]];
b1Lab8.text=st10;
b1Lab8.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab8];
b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];
NSString *st11 = [NSString stringWithFormat:#"1 YR: %#%%",[oneYearValue objectAtIndex:indexPath.section]];
b1Lab4.text=st11;
b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab4.numberOfLines = 0;
b1Lab4.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab4];
b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];
NSString *st12 = [NSString stringWithFormat:#"3 YR: %#%%",[threeYearValue objectAtIndex:indexPath.section]];
b1Lab5.text=st12;
b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab5.numberOfLines = 0;
b1Lab5.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab5];
b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];
NSString *st13 = [NSString stringWithFormat:#"5 YR: %#%%",[fiveYearValue objectAtIndex:indexPath.section]];
b1Lab6.text=st13;
b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab6.numberOfLines = 0;
b1Lab6.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab6];
cell.backgroundColor = [UIColor whiteColor];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
Do you have any idea how such a behavior can occur?
Try this..
- (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];
}
[[cell viewWithTag:111]removeFromSuperview];
[[cell viewWithTag:222]removeFromSuperview];
[[cell viewWithTag:333]removeFromSuperview];
[[cell viewWithTag:444]removeFromSuperview];
[[cell viewWithTag:555]removeFromSuperview];
[[cell viewWithTag:666]removeFromSuperview];
[[cell viewWithTag:777]removeFromSuperview];
if (flag == NO) {
b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];
// NSString *stri = [title objectAtIndex:indexPath.section];
b1Lab1.text=[[sortedArray mutableArrayValueForKey:companyNam]objectAtIndex:indexPath.section];
b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab1.numberOfLines = 0;
b1Lab1.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab1.tag = 111;
[cell addSubview:b1Lab1];
b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
NSString *st = [NSString stringWithFormat:#"YTD %#%%",[[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section]];
b1Lab2.textAlignment=NSTextAlignmentCenter;
b1Lab2.text=st;
b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab2.numberOfLines = 0;
b1Lab2.tag = 222;
b1Lab2.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
NSString *tem = [[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section];
// NSArray *testItems = [tem componentsSeparatedByString:#"%"];
//
// NSDecimalNumber *value = [testItems objectAtIndex:0];
float tempr = [tem floatValue];
if(tempr <0){
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_red_80x20.png"]];
}
else{
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_green_80x20.png"]];
}
[cell addSubview:b1Lab2];
b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
NSString *st1 = [NSString stringWithFormat:#"Net Asset Value Per Share"];
b1Lab3.text=st1;
b1Lab3.font = [UIFont fontWithName:#"Arial" size:13];
b1Lab2.tag=333;
[cell addSubview:b1Lab3];
b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
b1Lab8.textAlignment = NSTextAlignmentRight;
NSString *st10 = [NSString stringWithFormat:#"%#", [[sortedArray mutableArrayValueForKey:netVal]objectAtIndex:indexPath.section]];
b1Lab8.text=st10;
b1Lab8.font = [UIFont fontWithName:#"Arial" size:13];
b1Lab2.tag=444;
[cell addSubview:b1Lab8];
b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];
NSString *st11 = [NSString stringWithFormat:#"1 YR: %#%%",[[sortedArray mutableArrayValueForKey:oneYear]objectAtIndex:indexPath.section]];
b1Lab4.text=st11;
b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab4.numberOfLines = 0;
b1Lab4.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab2.tag=555;
[cell addSubview:b1Lab4];
b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];
NSString *st12 = [NSString stringWithFormat:#"3 YR: %#%%",[[sortedArray mutableArrayValueForKey:threeYear]objectAtIndex:indexPath.section]];
b1Lab5.text=st12;
b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab5.numberOfLines = 0;
b1Lab5.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab2.tag=666;
[cell addSubview:b1Lab5];
b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];
NSString *st13 = [NSString stringWithFormat:#"5 YR: %#%%",[[sortedArray mutableArrayValueForKey:fiveYear]objectAtIndex:indexPath.section]];
b1Lab6.text=st13;
b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab6.numberOfLines = 0;
b1Lab6.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab2.tag=777;
[cell addSubview:b1Lab6];
cell.backgroundColor = [UIColor whiteColor];
}
else if(flag == YES)
{
b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];
// NSString *stri = [title objectAtIndex:indexPath.section];
b1Lab1.text=[companyName objectAtIndex:indexPath.section];
b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab1.numberOfLines = 0;
b1Lab1.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab1.tag = 111;
[cell addSubview:b1Lab1];
b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
NSString *st = [NSString stringWithFormat:#"YTD %#%%",[ytdRate objectAtIndex:indexPath.section]];
b1Lab2.textAlignment=NSTextAlignmentCenter;
b1Lab2.text=st;
b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab2.numberOfLines = 0;
b1Lab2.tag = 222;
b1Lab2.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
NSString *tem = [ytdRate objectAtIndex:indexPath.section];
// NSArray *testItems = [tem componentsSeparatedByString:#"%"];
//
// NSDecimalNumber *value = [testItems objectAtIndex:0];
float tempr = [tem floatValue];
if(tempr <0){
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_red_80x20.png"]];
}
else{
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_green_80x20.png"]];
}
[cell addSubview:b1Lab2];
b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
NSString *st1 = [NSString stringWithFormat:#"Net Asset Value Per Share"];
b1Lab3.text=st1;
b1Lab3.font = [UIFont fontWithName:#"Arial" size:13];
b1Lab3.tag = 333;
[cell addSubview:b1Lab3];
b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
b1Lab8.textAlignment = NSTextAlignmentRight;
NSString *st10 = [NSString stringWithFormat:#"%#", [netAsset objectAtIndex:indexPath.section]];
b1Lab8.text=st10;
b1Lab8.font = [UIFont fontWithName:#"Arial" size:13];
b1Lab8.tag = 444;
[cell addSubview:b1Lab8];
b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];
NSString *st11 = [NSString stringWithFormat:#"1 YR: %#%%",[oneYearValue objectAtIndex:indexPath.section]];
b1Lab4.text=st11;
b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab4.numberOfLines = 0;
b1Lab4.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab4.tag = 555;
[cell addSubview:b1Lab4];
b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];
NSString *st12 = [NSString stringWithFormat:#"3 YR: %#%%",[threeYearValue objectAtIndex:indexPath.section]];
b1Lab5.text=st12;
b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab5.numberOfLines = 0;
b1Lab5.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab5.tag = 666;
[cell addSubview:b1Lab5];
b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];
NSString *st13 = [NSString stringWithFormat:#"5 YR: %#%%",[fiveYearValue objectAtIndex:indexPath.section]];
b1Lab6.text=st13;
b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab6.numberOfLines = 0;
b1Lab6.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
b1Lab6.tag = 777;
[cell addSubview:b1Lab6];
cell.backgroundColor = [UIColor whiteColor];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;}
tableView dequeueReusableCellWithIdentifier: means that you're requesting a UITableViewCell object of a certain type (identifier), and you'll get either a new or old one depending on whether or not a cell with your identifier has been created yet.
Your code requests a set of table cells and fills them with UILabels. When you scroll, your table view requests more table cells. At this point, since some cells of this type have already been created, you're getting the old objects - the old table cells with UILabels already in them - and then creating more UILabels within these cells. This is why they overlap.
You could stop using reusable cells, and simply create a new UITableViewCell for each table cell, however this is not recommended. This method would use significantly more memory as the number of table cells increases, which is the problem that reusable cells are intended to overcome. A more detailed explanation is available in this answer.
Instead, what you should do is simply clear all of the content from the reusable cells that you retrieve, ensuring that your cell is clear of any previously created content when you create your UILabels again. Adding the following code to your cellForRowAtIndexPath (after you've retrieved a reusable cell) method will do the trick:
for(UIView *view in [cell subviews])
[view removeFromSuperview];
- (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];}
for (UIView *v [cell.contenetView subviews]{
[v removefromSuperView];}
******* Change code *********
if (flag == NO) {
b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];
// NSString *stri = [title objectAtIndex:indexPath.section];
b1Lab1.text=[[sortedArray mutableArrayValueForKey:companyNam]objectAtIndex:indexPath.section];
b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab1.numberOfLines = 0;
b1Lab1.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab1];
b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
NSString *st = [NSString stringWithFormat:#"YTD %#%%",[[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section]];
b1Lab2.textAlignment=NSTextAlignmentCenter;
b1Lab2.text=st;
b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab2.numberOfLines = 0;
b1Lab2.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
NSString *tem = [[sortedArray mutableArrayValueForKey:ytd]objectAtIndex:indexPath.section];
// NSArray *testItems = [tem componentsSeparatedByString:#"%"];
//
// NSDecimalNumber *value = [testItems objectAtIndex:0];
float tempr = [tem floatValue];
if(tempr <0){
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_red_80x20.png"]];
}
else{
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_green_80x20.png"]];
}
[cell addSubview:b1Lab2];
b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
NSString *st1 = [NSString stringWithFormat:#"Net Asset Value Per Share"];
b1Lab3.text=st1;
b1Lab3.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab3];
b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
b1Lab8.textAlignment = NSTextAlignmentRight;
NSString *st10 = [NSString stringWithFormat:#"%#", [[sortedArray mutableArrayValueForKey:netVal]objectAtIndex:indexPath.section]];
b1Lab8.text=st10;
b1Lab8.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab8];
b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];
NSString *st11 = [NSString stringWithFormat:#"1 YR: %#%%",[[sortedArray mutableArrayValueForKey:oneYear]objectAtIndex:indexPath.section]];
b1Lab4.text=st11;
b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab4.numberOfLines = 0;
b1Lab4.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab4];
b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];
NSString *st12 = [NSString stringWithFormat:#"3 YR: %#%%",[[sortedArray mutableArrayValueForKey:threeYear]objectAtIndex:indexPath.section]];
b1Lab5.text=st12;
b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab5.numberOfLines = 0;
b1Lab5.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab5];
b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];
NSString *st13 = [NSString stringWithFormat:#"5 YR: %#%%",[[sortedArray mutableArrayValueForKey:fiveYear]objectAtIndex:indexPath.section]];
b1Lab6.text=st13;
b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab6.numberOfLines = 0;
b1Lab6.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab6];
cell.backgroundColor = [UIColor whiteColor];
}
else if(flag == YES)
{
b1Lab1=[[UILabel alloc] initWithFrame:CGRectMake(20,2,200,40)];
// NSString *stri = [title objectAtIndex:indexPath.section];
b1Lab1.text=[companyName objectAtIndex:indexPath.section];
b1Lab1.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab1.numberOfLines = 0;
b1Lab1.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab1];
b1Lab2=[[UILabel alloc] initWithFrame:CGRectMake(222,12,80,20)];
NSString *st = [NSString stringWithFormat:#"YTD %#%%",[ytdRate objectAtIndex:indexPath.section]];
b1Lab2.textAlignment=NSTextAlignmentCenter;
b1Lab2.text=st;
b1Lab2.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab2.numberOfLines = 0;
b1Lab2.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
NSString *tem = [ytdRate objectAtIndex:indexPath.section];
// NSArray *testItems = [tem componentsSeparatedByString:#"%"];
//
// NSDecimalNumber *value = [testItems objectAtIndex:0];
float tempr = [tem floatValue];
if(tempr <0){
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_red_80x20.png"]];
}
else{
b1Lab2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"val_green_80x20.png"]];
}
[cell addSubview:b1Lab2];
b1Lab3=[[UILabel alloc] initWithFrame:CGRectMake(20,35,180,20)];
NSString *st1 = [NSString stringWithFormat:#"Net Asset Value Per Share"];
b1Lab3.text=st1;
b1Lab3.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab3];
b1Lab8=[[UILabel alloc] initWithFrame:CGRectMake(200,35,95,20)];
b1Lab8.textAlignment = NSTextAlignmentRight;
NSString *st10 = [NSString stringWithFormat:#"%#", [netAsset objectAtIndex:indexPath.section]];
b1Lab8.text=st10;
b1Lab8.font = [UIFont fontWithName:#"Arial" size:13];
[cell addSubview:b1Lab8];
b1Lab4 = [[UILabel alloc] initWithFrame:CGRectMake(20,49,90,50)];
NSString *st11 = [NSString stringWithFormat:#"1 YR: %#%%",[oneYearValue objectAtIndex:indexPath.section]];
b1Lab4.text=st11;
b1Lab4.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab4.numberOfLines = 0;
b1Lab4.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab4];
b1Lab5=[[UILabel alloc] initWithFrame:CGRectMake(115,49,90,50)];
NSString *st12 = [NSString stringWithFormat:#"3 YR: %#%%",[threeYearValue objectAtIndex:indexPath.section]];
b1Lab5.text=st12;
b1Lab5.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab5.numberOfLines = 0;
b1Lab5.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab5];
b1Lab6=[[UILabel alloc] initWithFrame:CGRectMake(210,49,90,50)];
NSString *st13 = [NSString stringWithFormat:#"5 YR: %#%%",[fiveYearValue objectAtIndex:indexPath.section]];
b1Lab6.text=st13;
b1Lab6.lineBreakMode = NSLineBreakByWordWrapping;
b1Lab6.numberOfLines = 0;
b1Lab6.font = [UIFont fontWithName:#"Helvetica-Bold" size:13];
[cell addSubview:b1Lab6];
cell.backgroundColor = [UIColor whiteColor];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
NEW UPDATED ANSWER
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
}

UITableView Scrolling slowness issues when loading cell

I am having issues with the speed in which my tableview Cells scroll. You can see that it hangs up when dequeuing and reusing a cell. Below is my code that I use to create the cells. I am using 2 custom cells and one cell is if there is a image and the other cell is if the user didn't attach an image. Any insight would be greatly appreciated. I should also add that I wanted to add a space between the cells so basically I create a new section containing a single cell and that is why you will always see in my code indexPath.section.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *imageCell = #"ShameImage";
static NSString *noImageCell = #"ShameNoImageCell";
static NSString *noCell = #"NoCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:noCell];
if ([[blobID objectAtIndex:indexPath.section] isEqualToNumber:[NSNumber numberWithInt:1]])
{
ShameViewCell *cell = (ShameViewCell *)[self.tableView dequeueReusableCellWithIdentifier:imageCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameImageCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
CALayer *cellImageView = [cell.imageView layer];
[cellImageView setMasksToBounds:YES];
[cellImageView setCornerRadius:10.0];
IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
[iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
iiv.tag = 999;
[iiv loadImageFromURL];
[cell.imageView addSubview:iiv];
cell.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapImage = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(openPicture:)];
tapImage.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapImage];
[cell.contentView.layer setCornerRadius:10];
[cell.contentView setBackgroundColor:[UIColor blackColor]];
[cell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
cell.lastShame.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[cell.lastShame setTextColor:insideColor];
[cell.lastShame setFont:[UIFont fontWithName:text_font_name size:14]];
cell.lastShame.text = [userShame objectAtIndex:indexPath.section];
[cell.shameDate setTextColor:insideColor];
[cell.shameDate setFont:[UIFont fontWithName:text_font_name size:11]];
cell.shameDate.text = [createDt objectAtIndex:indexPath.section];
[cell.userLabel setTextColor:insideColor];
[cell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
cell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
[cell.lastShame setBackgroundColor:[UIColor clearColor]];
return cell;
}
}else
{
ShameNoImage *cell = (ShameNoImage *)[self.tableView dequeueReusableCellWithIdentifier:noImageCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameNoImageCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
CALayer *cellImageView = [cell.imageView layer];
[cellImageView setMasksToBounds:YES];
[cellImageView setCornerRadius:10.0];
[cellImageView setBorderWidth:1.0];
[cellImageView setBorderColor:[[UIColor whiteColor] CGColor]];
[cell.contentView.layer setCornerRadius:10];
[cell.contentView setBackgroundColor:[UIColor blackColor]];
[cell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
cell.shameLabel.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[cell.shameLabel setTextColor:insideColor];
[cell.shameLabel setFont:[UIFont fontWithName:text_font_name size:14]];
cell.shameLabel.text = [userShame objectAtIndex:indexPath.section];
[cell.dateLabel setTextColor:insideColor];
[cell.dateLabel setFont:[UIFont fontWithName:text_font_name size:11]];
cell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
[cell.userLabel setTextColor:insideColor];
[cell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
cell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
[cell.shameLabel setBackgroundColor:[UIColor clearColor]];
return cell;
}
}
return cell;
}
The first thing that jumps out are the calls to setCornerRadius. A quick test would be to cut out that corner radius stuff to see if that helps your speed. If that's you're problem you'll have to switch to drawing with CG and a UIBezierPath.
The next thing to look at would be transparency and the number of subviews you are using. It's hard to tell with what is posted above but the more subviews, especially with alpha are going to mean more work compositing. But that can be hard to get around depending on your design but drawing with CG or using images can help.
I figured it out. I was recreating and drawing every cell. I needed to set the reuseIdentifier in the Cell's Subclass and then change the building the the table to be like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *imageCell = #"ShameImage";
static NSString *noImageCell = #"ShameNoImageCell";
ShameViewCell *iCell = (ShameViewCell *)[self.tableView dequeueReusableCellWithIdentifier:imageCell];
ShameNoImage *niCell = (ShameNoImage *)[self.tableView dequeueReusableCellWithIdentifier:noImageCell];
if ([[blobID objectAtIndex:indexPath.section] isEqualToNumber:[NSNumber numberWithInt:1]])
{
if (iCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameImageCell" owner:self options:nil];
iCell = [nib objectAtIndex:0];
CALayer *cellImageView = [iCell.imageView layer];
[cellImageView setMasksToBounds:YES];
IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
[iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
iiv.tag = 999;
[iiv loadImageFromURL];
[iCell.imageView addSubview:iiv];
iCell.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapImage = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(openPicture:)];
tapImage.numberOfTapsRequired = 1;
[iCell.imageView addGestureRecognizer:tapImage];
[iCell.contentView setBackgroundColor:[UIColor blackColor]];
[iCell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
iCell.lastShame.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[iCell.lastShame setTextColor:insideColor];
[iCell.lastShame setFont:[UIFont fontWithName:text_font_name size:14]];
iCell.lastShame.text = [userShame objectAtIndex:indexPath.section];
[iCell.shameDate setTextColor:insideColor];
[iCell.shameDate setFont:[UIFont fontWithName:text_font_name size:11]];
iCell.shameDate.text = [createDt objectAtIndex:indexPath.section];
[iCell.userLabel setTextColor:insideColor];
[iCell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
iCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return iCell;
}else
{
[[iCell.imageView viewWithTag:999] removeFromSuperview];
IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
[iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
iiv.tag = 999;
[iiv loadImageFromURL];
[iCell.imageView addSubview:iiv];
iCell.lastShame.text = [userShame objectAtIndex:indexPath.section];
iCell.shameDate.text = [createDt objectAtIndex:indexPath.section];
iCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return iCell;
}
}else
{
if (niCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameNoImageCell" owner:self options:nil];
niCell = [nib objectAtIndex:0];
[niCell.contentView setBackgroundColor:[UIColor blackColor]];
[niCell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
niCell.shameLabel.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[niCell.shameLabel setTextColor:insideColor];
[niCell.shameLabel setFont:[UIFont fontWithName:text_font_name size:14]];
niCell.shameLabel.text = [userShame objectAtIndex:indexPath.section];
[niCell.dateLabel setTextColor:insideColor];
[niCell.dateLabel setFont:[UIFont fontWithName:text_font_name size:11]];
niCell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
[niCell.userLabel setTextColor:insideColor];
[niCell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
niCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return niCell;
}else
{
niCell.shameLabel.text = [userShame objectAtIndex:indexPath.section];
niCell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
niCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return niCell;
}
}
return niCell;
}

how to place star rating image next to the textlabel

i m parsing json feed and according to the parsed value i m placing star images in table view cell.what i want is to place the star rating images as soon as the no of words in the textlabel gets completed the star rating images got to be flexible and not to have fixed frame. below is the code and screenshot of how the output got to look like.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
static NSString *CellIdentifier = #"Cell";
HJManagedImageV* mi;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
mi = [[[HJManagedImageV alloc] initWithFrame:CGRectMake(-18,-2,90,90)] autorelease];
mi.tag = 999;
[cell addSubview:mi];
}
else
{
mi=(HJManagedImageV *)[cell viewWithTag:999];
[mi clear];
}
NSDictionary *boy=[self.media1 objectAtIndex:indexPath.row];
NSString *str=[[NSString alloc]initWithFormat:#"%#",boy];
NSInteger n=[str intValue];
NSLog(#"the value:%#",str);
if(n ==0)
{
CGRect starFrame = CGRectMake(100,6, 85, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"nostar.png"];
starImage.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
CGRect labelFrame = CGRectMake(290,18, 40, 40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
// Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
if(n >=1)
{
CGRect starFrame = CGRectMake(100,6, 85, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"1star.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
CGRect labelFrame = CGRectMake(290,18, 40, 40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
Label.text=str;
Label.tag=1000;
//Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
if(n >=2)
{
CGRect starFrame = CGRectMake(100,6, 85, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"twostar.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
CGRect labelFrame = CGRectMake(290,18, 40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
// Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
if(n >=3)
{
CGRect starFrame = CGRectMake(100,6, 85, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"threestar.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
CGRect labelFrame = CGRectMake(290,18, 40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
if(n >= 4)
{
CGRect starFrame = CGRectMake(100,6, 85, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"4star.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
CGRect labelFrame = CGRectMake(290,18,40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
//Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
if(n >= 5)
{
CGRect starFrame = CGRectMake(100,6, 85, 40);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"5star.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
CGRect labelFrame = CGRectMake(290,18, 40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.font = [UIFont systemFontOfSize:18];
Label.text=str;
Label.tag=1000;
// Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
cell.textLabel.text=[self.story objectAtIndex:indexPath.row];
cell.textLabel.numberOfLines=2;
return cell;
}
try out this code, i have solved
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
static NSString *CellIdentifier = #"Cell";
HJManagedImageV* mi;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
mi = [[[HJManagedImageV alloc] initWithFrame:CGRectMake(-18,-2,90,90)] autorelease];
mi.tag = 999;
[cell addSubview:mi];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
CGSize sizeTitle = [[self.story objectAtIndex:indexPath.row] sizeWithFont:[UIFont boldSystemFontOfSize:16.0]
constrainedToSize:CGSizeMake(200, 40)
lineBreakMode:UILineBreakModeWordWrap];
float width = sizeTitle.width;
NSLog(#"Width %f", width);
width = width+15;
NSDictionary *boy=[self.media1 objectAtIndex:indexPath.row];
NSString *str=[[NSString alloc]initWithFormat:#"%#",boy];
NSInteger n=[str intValue];
NSLog(#"the value:%#",str);
CGRect coreFrame;
if(width<170){
coreFrame = CGRectMake(2, 6, width, 40);
}
else{
coreFrame = CGRectMake(2, 6, 170, 40);
width = 175;
}
float k = width+85;
UILabel *lab = [[UILabel alloc] initWithFrame:coreFrame];
lab.text = [self.story objectAtIndex:indexPath.row];
[cell.contentView addSubview:lab];
if(n ==0)
{
CGRect starFrame = CGRectMake(width,6, 85, 37);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"nostar.png"];
starImage.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
// str=[str substringToIndex:4];
CGRect labelFrame = CGRectMake(k,6, 40, 40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
Label.textColor=[UIColor grayColor];
[cell.contentView addSubview:Label];
}
if(n >=1)
{
CGRect starFrame = CGRectMake(width,6, 85, 37);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"1star.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
CGRect labelFrame = CGRectMake(k,6, 40, 40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
Label.text=str;
Label.tag=1000;
Label.textColor=[UIColor grayColor];
[cell.contentView addSubview:Label];
}
if(n >=2)
{
CGRect starFrame = CGRectMake(width,6, 85, 37);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"twostar.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
CGRect labelFrame = CGRectMake(k,6, 40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
Label.textColor=[UIColor grayColor];
// Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
if(n >=3)
{
CGRect starFrame = CGRectMake(width,6, 85, 37);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"threestar.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
// str=[str substringToIndex:4];
CGRect labelFrame = CGRectMake(k,6, 40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
Label.textColor=[UIColor grayColor];
[cell.contentView addSubview:Label];
}
if(n >= 4)
{
CGRect starFrame = CGRectMake(width,6, 85, 37);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"4star.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
//str=[str substringToIndex:4];
CGRect labelFrame = CGRectMake(k,6,40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.text=str;
Label.tag=1000;
Label.textColor=[UIColor grayColor];
//Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
if(n >= 5)
{
CGRect starFrame = CGRectMake(width,6, 85, 37);
UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
starImage.image = [UIImage imageNamed:#"5star.png"];
[cell.contentView addSubview:starImage];
NSString *boo=[[NSString alloc]initWithFormat:#"%d",n];
NSString *str=[[NSString alloc]initWithFormat:#"(%#)",boo];
// str=[str substringToIndex:4];
CGRect labelFrame = CGRectMake(k,6, 40,40);
UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
Label.font = [UIFont systemFontOfSize:18];
Label.text=str;
Label.tag=1000;
Label.textColor=[UIColor grayColor];
// Label.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:Label];
}
return cell;
}
You can use the following method to calculate the width and height of a string --
– sizeWithFont:
– sizeWithFont:forWidth:lineBreakMode:
– sizeWithFont:constrainedToSize:
– sizeWithFont:constrainedToSize:lineBreakMode:
– sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
or you can refer the doc here https://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html
on the basis of width you need to set your label width and then set the frame of your star image.
an example -
CGSize sizeTitle = [titleStr sizeWithFont:[UIFont boldSystemFontOfSize:16.0]
                                      constrainedToSize:CGSizeMake(200, 40)
                                          lineBreakMode:UILineBreakModeWordWrap];
float width = sizeTitle.width;

Crash when scrolling the uitableview

I have a uitableview which crashes when scrolling.
I have tried leaks and build and analyse but could not solve this.
getting the below error
*** -[CALayer retain]: message sent to deallocated instance 0xe192280
Below is the code i am running.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIFont *font = [UIFont fontWithName:#"Arial" size:13];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MyIdentifier"] autorelease];
NSInteger row = [indexPath row];
cellRect2 = CGRectMake(135, 10, 5, 20 );
lblColon = [[UILabel alloc] initWithFrame: cellRect2];
lblColon.font = font;
lblColon.backgroundColor = [UIColor clearColor];
lblColon.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblColon];
lblColon.text = #":";
if (tableView == tblCompanyDetails) {
switch (indexPath.section)
{
case 0:
{
profileName = [appDelegate.arrCompanyProfile1 objectAtIndex:indexPath.row];
NSLog(#"appDelegate.arrCompanyProfile1 ------> %#", appDelegate.arrCompanyProfile1);
if(row == 0)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Company Name";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *str = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
//NSLog(#"str--------%#", str);
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 1)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Type of Company";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSLog(#"profileName.namelast--------%#", profileName);
//NSString *strLastName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 2)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Main Contact Person";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 3)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Address";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 4)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"City";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 5)
{
cellRect1 = CGRectMake(10, 10, 100, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"State";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 6)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Postal / Zip Code";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 7)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Country";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 8)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Primary Phone No.";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 9)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Mobile No.";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 10)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Toll Free No.";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 11)
{
cellRect1 = CGRectMake(10, 10, 100, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Websites";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet : [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 12)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Email Address";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet : [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 13)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Number of Employees";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet : [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 14)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Legal structure";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet : [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
}
case 1:
{
profileName = [appDelegate.arrCompanyProfile2 objectAtIndex:indexPath.row];
if(row == 0)
{
cellRect1 = CGRectMake(10, 10, 100, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Primary";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 1)
{
cellRect1 = CGRectMake(10, 10, 120, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Secondary";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSLog(#"profileName section2 ----> %#", profileName);
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 2)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"No of years in business";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
}
case 2:
{
profileName = [appDelegate.arrCompanyProfile3 objectAtIndex:indexPath.row];
if(row == 0)
{
cellRect1 = CGRectMake(10, 10, 130, 20 );
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Postal / Zip Code";
cellRect3 = CGRectMake( 145, 10, 180 ,20 );
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
if(row == 1)
{
cellRect1 = CGRectMake(10, 10, 130, 20);
lblTitle = [[UILabel alloc] initWithFrame: cellRect1];
//UIFont *font = [UIFont fontWithName:#"Arial" size:13];
lblTitle.font = font;
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblTitle];
lblTitle.text = #"Coverage Area (miles)";
cellRect3 = CGRectMake(145, 10, 180 ,20);
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
//NSString *strFirstName = [profileName stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
}
case 3:
{
profileName = [appDelegate.arrCompanyProfile4 objectAtIndex:indexPath.row];
if(row == 0)
{
lblColon.hidden = YES;
cellRect3 = CGRectMake(10, 10, 280, 40);
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
//UIFont *font = [UIFont fontWithName:#"Arial-BoldMT" size:14];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
}
case 4:
{
profileName = [appDelegate.arrCompanyProfile5 objectAtIndex:indexPath.row];
if(row == 0)
{
lblColon.hidden = YES;
cellRect3 = CGRectMake(10, 10, 100, 20);
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
[lblResult setText:[NSString stringWithFormat: #"%# ", profileName]];
break;
}
}
case 5:
{
if(row == 0)
{
lblColon.hidden = YES;
cellRect3 = CGRectMake(10, 10, 100, 20);
lblResult = [[UILabel alloc] initWithFrame: cellRect3];
lblResult.font = font;
lblResult.backgroundColor = [UIColor clearColor];
lblResult.textColor = [UIColor blackColor];
[cell.contentView addSubview: lblResult];
break;
}
}
}
}
}
return cell;
}
You are not actually reusing any cell because you are doing all part within the cell== nil code bloack, Changing the code something like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIFont *font = [UIFont fontWithName:#"Arial" size:13];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MyIdentifier"] autorelease];
UILabel* lblColon = [[UILabel alloc] initWithFrame:CGRectMake(135, 10, 5, 20 )];
lblColon.font = font;
lblColon.backgroundColor = [UIColor clearColor];
lblColon.textColor = [UIColor blackColor];
lblColon.text = #":";
[lblColon setTag:222];
[cell.contentView addSubview: lblColon];
[lblColon release];
}
NSInteger row = [indexPath row];
UILabel* lblToReuse = (UILabel*)[cell.contentView viewWithTag:222];
if (tableView == tblCompanyDetails) {
//usual things you were doing earlier
}
return cell;
}
might help you a bit. Check it if it still crashes after replacing the code with this one.
Where are you releasing all the objects you are allocating? It looks like you are trying to alloc the same objects over and over.
try the following approach:
UILabel *lblTitle = [[[UILabel alloc] initWithFrame: cellRect1] autorelease];
At first, mechanism of reusing cell doesn't work in your code, because you do:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// ...
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MyIdentifier"] autorelease];
You are trying to reuse cell with identifier "Cell"? but when you create cell you create it with reuseIdentifier "MyIdentifier". It's bad. You should create cell with the same identifier:
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
And move your part for individually configuring of cell (depending on indexPath) outside if (cell == nil) { statement. Inside this statement you should only global configuring for your cells. Configure here only the same things for all cells in this tanle view. Because this cell will be reused for other cells.
When we are using reusing mechanism for cells it try to find cell with identifier (calling [tableView dequeueReusableCellWithIdentifier:CellIdentifier]). If it's not found, than you create cell prototype with the same identifier
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MyIdentifier"] autorelease];
}
and after this part of code place your specific code for current cell (at specified indexPath)

iPhone Problem with custom cell

I try this code but it's a mistake because i have a repetition of title and description...
Someone can help me please?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomCell *cell=(CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[[CustomCell alloc]initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else{
AsyncImageView* oldImage = (AsyncImageView*)
[cell.contentView viewWithTag:999];
[oldImage removeFromSuperview];
}
NSString *mediaUrl = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]mediaUrl];
NSString *noimage=#"http://www.notizie-informatiche.com/wp-content/themes/arthemia/images/imagelogo.png";
if([mediaUrl isEqualToString:noimage] == FALSE){
//DISEGNO IL FRAME PER L'IMMAGINE
CGRect frameimmagine;
frameimmagine.size.width=100; frameimmagine.size.height=120;
frameimmagine.origin.x=0; frameimmagine.origin.y=0;
AsyncImageView* asyncImage = [[[AsyncImageView alloc] initWithFrame:frameimmagine] autorelease];
UIImage *myImage = [UIImage imageNamed:#"uknown.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
[cell setAccessoryView:imageView];
//SCARICO L'IMMAGINE
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
asyncImage.tag = 999;
NSURL *url = [NSURL URLWithString: mediaUrl];
[asyncImage loadImageFromURL:url];
[cell.contentView addSubview:asyncImage];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
//OTTENGO IL TITOLO
//cell.lbltitolo = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]title];
NSString *titolo = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]title];
//OTTENFO LA DESCRIZIONE
//cell.lblsottotitolo.text = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]description];
NSString *descrizione = [[[[self rssParser]rssItems]objectAtIndex:indexPath.row]description];
UILabel *cellatitoloconimmagine=[[UILabel alloc]init];
[cellatitoloconimmagine removeFromSuperview];
UILabel *cellatitolo=[[UILabel alloc]init];
[cellatitolo removeFromSuperview];
UILabel *celladescrizione=[[UILabel alloc]init];
[celladescrizione removeFromSuperview];
UILabel *celladescrizioneconimmagine=[[UILabel alloc]init];
[celladescrizioneconimmagine removeFromSuperview];
/*
[celladescrizioneconimmagine removeFromSuperview];
[celladescrizione removeFromSuperview];
*/
if([mediaUrl isEqualToString:noimage] == FALSE){
CGRect frametitoloconimmagine = CGRectMake(105.0f, 5.0f, 210, 40);
cellatitoloconimmagine.frame=frametitoloconimmagine;
cellatitoloconimmagine.text=titolo;
cellatitoloconimmagine.textAlignment = UITextAlignmentLeft;
cellatitoloconimmagine.font = [UIFont boldSystemFontOfSize:16];
cellatitoloconimmagine.numberOfLines = 2;
[cell.contentView addSubview:cellatitoloconimmagine];
CGRect framedescrizioneconimmagine = CGRectMake(105.0f, 55.0f, 210, 50);
celladescrizioneconimmagine.frame=framedescrizioneconimmagine;
celladescrizioneconimmagine.text=descrizione;
celladescrizioneconimmagine.textAlignment = UITextAlignmentLeft;
celladescrizioneconimmagine.font = [UIFont systemFontOfSize:14];
celladescrizioneconimmagine.numberOfLines = 3;
[cell.contentView addSubview:celladescrizioneconimmagine];
}
else {
CGRect frametitolo = CGRectMake(5.0f, 5.0f, 310, 40);
cellatitolo.frame=frametitolo;
cellatitolo.text=titolo;
cellatitolo.textAlignment = UITextAlignmentLeft;
cellatitolo.font = [UIFont boldSystemFontOfSize:16];
cellatitolo.numberOfLines = 2;
[cell.contentView addSubview:cellatitolo];
CGRect framedescrizione = CGRectMake(5.0f, 55.0f, 310, 50);
celladescrizione.frame=framedescrizione;
celladescrizione.textAlignment = UITextAlignmentLeft;
celladescrizione.font = [UIFont systemFontOfSize:14];
celladescrizione.numberOfLines = 3;
celladescrizione.text=descrizione;
[cell.contentView addSubview:celladescrizione];
}
return cell;
}
the cell identifier must be unique for unique cells. It seems you have repeated cells because they are all dequed with name #"cell". Also remove the "static" word.