Related
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];
}
Here's my code :-
I need to Change only the first letter of the Word to capitalized.
UILabel *lbl4=[[UILabel alloc]initWithFrame:CGRectMake(10, 90, 200, 30)];
lbl4.text= [NSString stringWithFormat:#"(%#)", [arrtype objectAtIndex:0]];
lbl4.font = [UIFont fontWithName:#"Helvetica" size:18];
lbl4.textColor=[UIColor blackColor];
lbl4.backgroundColor=[UIColor clearColor];
Capitalization is for string not label
use
[NSString capitalizedString]
to capitalise a string and set it as text to the label
lbl4.text= [[NSString stringWithFormat:#"(%#)", [arrtype objectAtIndex:0]]capitalizedString];
NSString *abc = #"demo";
abc = [NSString stringWithFormat:#"%#%#",[[abc substringToIndex:1] uppercaseString],[abc substringFromIndex:1] ];
NSLog(#"abc = %#",abc);
Use this
UILabel *lbl4=[[UILabel alloc]initWithFrame:CGRectMake(10, 90, 200, 30)];
NSString *abc = [NSString stringWithFormat:#"(%#)", [arrtype objectAtIndex:0]];
abc = [NSString stringWithFormat:#"%#%#",[[abc substringToIndex:1] uppercaseString],[abc substringFromIndex:1]];
lbl4.text = abc;
lbl4.font = [UIFont fontWithName:#"Helvetica" size:18];
lbl4.textColor=[UIColor blackColor];
lbl4.backgroundColor=[UIColor clearColor];
- (NSString *) capitalizedFirstLetter:(NSString *)stringToConvert
{
NSString *retVal = stringToConvert;
if (stringToConvert.length <= 1)
{
retVal = stringToConvert.capitalizedString;
}else
{
retVal= [NSString stringWithFormat:#"%#%#",[[retVal substringToIndex:1] uppercaseString],[retVal substringFromIndex:1] ];
}
return retVal;
}
call this method:
[self capitalizedFirstLetter:labelName or textFieldName];
I am new to i phone programming.I have store images in array then using below code i am displaying images in thumbnail its working very fast.If same think i have store images in database and retrieving images and storing in array then using below code i am displaying images in thumbnail its taking too long to displaying thumbnail images.
arrayz = [NSMutableArray arrayWithObjects:[UIImage imageNamed:#"image3.jpg"],
[UIImage imageNamed:#"image4.jpg"],
[UIImage imageNamed:#"image5.jpg"],
[UIImage imageNamed:#"image3.jpg"],
[UIImage imageNamed:#"image5.jpg"],
[UIImage imageNamed:#"image4.jpg"],
[UIImage imageNamed:#"image2.jpg"],
[UIImage imageNamed:#"image3.jpg"],
[UIImage imageNamed:#"image2.jpg"],
[UIImage imageNamed:#"image5.jpg"],
nil];
NSLog(#"%i" , [arrayz count]);
NSLog(#"%#",arrayz);
myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 840.0)];
myScrollView.delegate = self;
myScrollView.contentSize = CGSizeMake(320.0, 840.0);
myScrollView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myScrollView];
float horizontal = 8.0;
float vertical = 8.0;
for(int i=0; i<[arrayz count]; i++)
{
if((i%4) == 0 && i!=0)
{
horizontal = 8.0;
vertical = vertical + 70.0 + 8.0;
}
buttonImage1 = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonImage1 setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)];
[buttonImage1 setTag:i];
[buttonImage1 setImage:[arrayz objectAtIndex:i] forState:UIControlStateNormal];
[buttonImage1 addTarget:self action:#selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonImage1 setImage:[UIImage imageNamed:#"Overlay.png"] forState:UIControlStateSelected];
buttonImage = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonImage setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)];
[buttonImage setTag:i];
[buttonImage setImage:[arrayz objectAtIndex:i] forState:UIControlStateNormal];
[buttonImage addTarget:self action:#selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonImage setImage:[UIImage imageNamed:#"Overlay.png"] forState:UIControlStateSelected];
// UIImageView *imageee=[[UIImageView alloc]initWithFrame:CGRectMake(10,10, 80,80)];
// [buttonImage setImage:[UIImage imageNamed:#"check.jpg"]];
// [buttonImage setImage:imageee forState:UIControlStateSelected];
// [buttonImage setImage:button.currentImage forState:UIControlStateNormal];
[myScrollView addSubview:buttonImage1];
[myScrollView addSubview:buttonImage];
horizontal = horizontal + 70.0 + 8.0;
[myScrollView setContentSize:CGSizeMake(320.0, vertical + 78.0)];
}
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
// UIBarButtonItem *done =[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = done;
below code using database i am storing images in array then i am displaying images in thumbnail.its taking too long to display the images in thumbnail.
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
array=[[NSMutableArray alloc]init];
array1=[[NSMutableArray alloc]init];
// array2=[[NSMutableArray alloc]init];
// Build the path to the database file
databasePath = [docsDir stringByAppendingPathComponent: #"Taukydataaa.db"];
// NSLog(#"%#",databasePath);
NSFileManager *fn=[NSFileManager defaultManager];
NSError *error;
BOOL success=[fn fileExistsAtPath:databasePath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Taukydataaa.db"];
success = [fn copyItemAtPath:defaultDBPath toPath:databasePath error:&error];
}
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: #"select * from path "];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while(sqlite3_step(statement) == SQLITE_ROW)
{
NSString* email_idField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,1)];
// NSLog(#"%#",email_idField);
NSString* email_idField1 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,0)];
// NSLog(#"%#",email_idField1);
// NSString *email_idField2 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement,2)];
NSLog(#"ASlma");
NSLog(#"%#",email_idField1);
[array addObject:email_idField];
[array1 addObject:email_idField1];
NSLog(#"%#",array);
NSLog(#"%#",array1);
// [array1 addObject:email_idField1];
// [array2 addObject:email_idField2];
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
}
blaukypath =[[NSMutableArray alloc]init];
for (NSString* path in array)
{
[blaukypath addObject:[UIImage imageWithContentsOfFile:path]];
NSLog(#"%#",path);
}
NSLog(#"%#",blaukypath1);
myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 840.0)];
myScrollView.delegate = self;
myScrollView.contentSize = CGSizeMake(320.0, 840.0);
myScrollView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myScrollView];
float horizontal = 8.0;
float vertical = 8.0;
for(int i=0; i<[blaukypath count]; i++)
{
if((i%4) == 0 && i!=0)
{
horizontal = 8.0;
vertical = vertical + 70.0 + 8.0;
}
buttonImage1 = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonImage1 setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)];
[buttonImage1 setTag:i];
[buttonImage1 setImage:[blaukypath objectAtIndex:i] forState:UIControlStateNormal];
[buttonImage1 addTarget:self action:#selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonImage1 setImage:[UIImage imageNamed:#"Overlay.png"] forState:UIControlStateSelected];
buttonImage = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonImage setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)];
[buttonImage setTag:i];
[buttonImage setImage:[blaukypath objectAtIndex:i] forState:UIControlStateNormal];
[buttonImage addTarget:self action:#selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside];
[buttonImage setImage:[UIImage imageNamed:#"Overlay.png"] forState:UIControlStateSelected];
// UIImageView *imageee=[[UIImageView alloc]initWithFrame:CGRectMake(10,10, 80,80)];
// [buttonImage setImage:[UIImage imageNamed:#"check.jpg"]];
// [buttonImage setImage:imageee forState:UIControlStateSelected];
// [buttonImage setImage:button.currentImage forState:UIControlStateNormal];
[myScrollView addSubview:buttonImage1];
[myScrollView addSubview:buttonImage];
horizontal = horizontal + 70.0 + 8.0;
[myScrollView setContentSize:CGSizeMake(320.0, vertical + 78.0)];
}
I think problem is not with database.What main think is if i take images from i phone real device that images taking too long to display in thumbnail.If i take some images and store in array that images displaying in thumbnail its working very fast..
Can any body tell me How to slove this.
Thanks
Aslam
Real images will have more size, so better you resize them before use. Try this one
UIImage *originalImage = your orginal Image
CGSize destinationSize = CGSizeMake(100, 120);// your destination thumbnail size
UIGraphicsBeginImageContext(destinationSize);
[originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
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)
I have a scroll view containing webview and webview is displaying content from xml. for e.g i have 10 pages . so if a want to go particular page how should i go to that page
int j=myindex;
int k=0;
for (int i=0; i < [appDelegate.books count]; i++) {
if (j>i) {
// //j++;
continue;
}
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * k;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView*subView=[[UIView alloc ]initWithFrame:frame];
//subView.backgroundColor=[appDelegate.books objectAtIndex:i];
[self.scrollView addSubview:subView];
CGRect myframe=CGRectMake(8, 16, 371, 242);
UIImageView*imageView=[[UIImageView alloc] initWithFrame:myframe];
Book*aBook=[appDelegate.books objectAtIndex:i];
NSString*myimage=aBook.mainimgurl;
myimage = [myimage stringByReplacingOccurrencesOfString:#"\n" withString:#""];
myimage = [myimage stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:myimage]]];
j++;
imageView.image=image;
CGRect mywebframe=CGRectMake(0, 30, 768, 876);
UIWebView*webView=[[UIWebView alloc] initWithFrame:mywebframe];
/*NSString*title=aBook.title;
NSString*mytitle=#"Rask og grønn";
title = [title stringByReplacingOccurrencesOfString:#"\n" withString:#""];
title = [title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString*mytitle1=#"Fortidsklimaet på Svalbard";
if ([title isEqualToString:mytitle ] || [title isEqualToString:mytitle1]) {
htmlString=aBook.magazinenumberdescription;
}*/
if (aBook.fulltext==nil) {
htmlString=aBook.magazinenumberdescription;
}
else {
htmlString=aBook.fulltext;
}
//NSString*htmlString=aBook.description;
[webView loadHTMLString:htmlString baseURL:nil];
[subView addSubview:webView];
CGRect myframe1=CGRectMake(387, 16, 363, 292);
UITextView*textView=[[UITextView alloc] initWithFrame:myframe1];
textView.font=[UIFont fontWithName:#"Times New Roman" size:16];
NSString*description=aBook.description;
description = [description stringByReplacingOccurrencesOfString:#"\n" withString:#""];
description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
description = [description stringByReplacingOccurrencesOfString:#"å" withString:#"å"];
description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
description = [description stringByReplacingOccurrencesOfString:#"ø" withString:#"ø"];
description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
description = [description stringByReplacingOccurrencesOfString:#"æ" withString:#"æ"];
description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
description = [description stringByReplacingOccurrencesOfString:#"Æ" withString:#"Æ"];
description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
description = [description stringByReplacingOccurrencesOfString:#"Ø" withString:#"Ø"];
description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
description = [description stringByReplacingOccurrencesOfString:#"Å" withString:#"Å"];
description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
k++;
textView.text=description;
// [subView addSubview:textView];
// [subView addSubview:imageView];
}
//NSInteger numberofPages=10-j;
NSInteger numberofPages=[appDelegate.books count];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * numberofPages, self.scrollView.frame.size.height);
[pageControl setNumberOfPages:numberofPages];
[pageControl setActivePageColor:[UIColor clearColor]];
[pageControl setInactivePageColor:[UIColor clearColor]];
try this :
- (void) viewDidLoad // Or any other method
{
[myScroll scrollRectToVisible:CGRectMake(320*pageNumber, 0, 320 , 240) animated:NO];
//320 is a width of my page,you have to replace it with your page's width
[self.view addSubview:myScroll];
}
hope this helps you..