Overlapping Table cells-content [closed] - iphone

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];
}

Related

How to capitalized first letter of Label

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];

Error reloading custom cell after changing data

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

TableViewCell to auto-resize its height according to content in xcode?

I need to set the height of tableViewCell according to content in it. I have some labels of dynamic height in tableViewCell,number of lines in the label is not constant. Sometimes if a one label's text is null then the other label may occupy its position. So what i need is tableViewCell should autoresize height according to the content?
-(void)viewDidLoad
{
self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,150,327,[array count]*205) style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
self.tableView.scrollEnabled = NO;
[self.view addSubview:self.tableView];
float fscrview = 150 + self.tableView.frame.size.height + 20;
testscroll.contentSize=CGSizeMake(320, fscrview);
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"Cell";
UITableViewCell* cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
UILabel *name1 = [[UILabel alloc] initWithFrame:CGRectZero];
name1.tag = 111;
name1.backgroundColor = [UIColor clearColor];
[name1 setFont:[UIFont fontWithName:#"Helvetica-Bold" size:14]];
[name1 setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:name1];
UILabel *code = [[UILabel alloc] initWithFrame:CGRectZero];
codetype.tag = 112;
codetype.backgroundColor = [UIColor clearColor];
[codetype setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[codetype setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:codetype];
line1 = [[UILabel alloc] initWithFrame:CGRectZero];
line1.tag = 113;
line1.backgroundColor = [UIColor clearColor];
[line1 setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[line1 setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:line1];
line3 = [[UILabel alloc] initWithFrame:CGRectZero];
line3.tag = 114;
line3.backgroundColor = [UIColor clearColor];
[line3 setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[line3 setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:line3];
city = [[UILabel alloc] initWithFrame:CGRectZero];
city.tag = 115;
city.backgroundColor = [UIColor clearColor];
[city setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[city setLineBreakMode:UILineBreakModeWordWrap];
[cell addSubview:city];
}
cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
NSMutableDictionary *d =[[NSMutableDictionary alloc]initWithDictionary: [arr2 objectAtIndex:indexPath.row]];
NSString *name2 = [d objectForKey:#"Name"];
CGSize constraint1 = CGSizeMake(175, 2000.0f);
CGSize size1 = [name2 sizeWithFont: [UIFont fontWithName:#"Helvetica-Bold" size:14] constrainedToSize:constraint1 lineBreakMode:UILineBreakModeWordWrap];
UILabel *name1 = (UILabel *)[cell viewWithTag:111];
name1.frame = CGRectMake(105,25, 175, size1.height);
[name1 setNumberOfLines:size1.height/16];
name1.text = [d objectForKey:#"Name"];
[name1 setTextColor:UIColorFromRGB(COLOR_BLUE)];
NSString *codetype2 = [d objectForKey:#"Type"];
CGSize constraint2 = CGSizeMake(175, 2000.0f);
CGSize size2 = [codetype2 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint2 lineBreakMode:UILineBreakModeWordWrap];
UILabel *codetype1 = (UILabel *)[cell viewWithTag:112];
codetype1.frame = CGRectMake(105,name1.frame.size.height+25, 175, size2.height);
[codetype1 setNumberOfLines:size2.height/16];
codetype1.text=[d objectForKey:#"CodeType"];
NSString *line2 = [d objectForKey:#"Line1"];
NSString *line4 = [d objectForKey:#"Line2"];
if([line2 isEqualToString:#""])
{
if([line4 isEqualToString:#""])
{
NSString *city2 = [d objectForKey:#"City"];
CGSize constraint4 = CGSizeMake(175, 2000.0f);
CGSize size4 = [city2 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
city = (UILabel *)[cell viewWithTag:115];
city.frame = CGRectMake(105,codetype1.frame.size.height+codetype1.frame.origin.y, 175, size4.height);
[city setNumberOfLines:0];
city.text = [d objectForKey:#"City"];
}
else
{
NSString *line4 = [d objectForKey:#"Line2"];
CGSize constraint4 = CGSizeMake(175, 2000.0f);
CGSize size4 = [line4 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
line3 = (UILabel *)[cell viewWithTag:114];
line3.frame = CGRectMake(105,codetype1.frame.size.height+codetype1.frame.origin.y, 175, size4.height);
[line3 setNumberOfLines:0];
line3.text = [d objectForKey:#"Line2"];
NSString *city2 = [d objectForKey:#"City"];
CGSize constraint5 = CGSizeMake(175, 2000.0f);
CGSize size5 = [city2 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap];
city = (UILabel *)[cell viewWithTag:115];
city.frame = CGRectMake(105,line3.frame.size.height+line3.frame.origin.y, 175, size5.height);
[city setNumberOfLines:0];
city.text = [d objectForKey:#"City"];
}
}
else
{
NSString *line2 = [d objectForKey:#"Line1"];
CGSize constraint3 = CGSizeMake(175, 2000.0f);
CGSize size3 = [line2 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint3 lineBreakMode:UILineBreakModeWordWrap];
line1 = (UILabel *)[cell viewWithTag:113];
line1.frame = CGRectMake(105,codetype1.frame.size.height+codetype1.frame.origin.y, 175, size3.height);
[line1 setNumberOfLines:0];
line1.text = [d objectForKey:#"Line1"];
if([line4 isEqualToString:#""])
{
NSString *city2 = [d objectForKey:#"City"];
CGSize constraint5 = CGSizeMake(175, 2000.0f);
CGSize size5 = [city2 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap];
city = (UILabel *)[cell viewWithTag:115];
city.frame = CGRectMake(105,line1.frame.size.height+line1.frame.origin.y, 175, size5.height);
[city setNumberOfLines:0];
city.text = [d objectForKey:#"City"];
}
else
{
NSString *line4 = [d objectForKey:#"Line2"];
CGSize constraint4 = CGSizeMake(175, 2000.0f);
CGSize size4 = [line4 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint4 lineBreakMode:UILineBreakModeWordWrap];
line3 = (UILabel *)[cell viewWithTag:114];
line3.frame = CGRectMake(105,line1.frame.size.height+line1.frame.origin.y, 175, size4.height-3);
[line3 setNumberOfLines:0];
line3.text = [d objectForKey:#"Line2"];
NSString *city2 = [d objectForKey:#"City"];
CGSize constraint5 = CGSizeMake(175, 2000.0f);
CGSize size5 = [city2 sizeWithFont: [UIFont fontWithName:#"Verdana" size:12] constrainedToSize:constraint5 lineBreakMode:UILineBreakModeWordWrap];
city = (UILabel *)[cell viewWithTag:115];
city.frame = CGRectMake(105,line3.frame.size.height+line3.frame.origin.y+4, 175, size5.height);
[city setNumberOfLines:0];
city.text = [d objectForKey:#"City"];
}
}
UILabel *state=[[UILabel alloc]initWithFrame:CGRectMake(105,city.frame.size.height+city.frame.origin.y+5,320,10)];
state.font=[UIFont fontWithName:#"Helvetica" size:12];
[state setTextAlignment:UITextAlignmentLeft];
[state setText:[d valueForKey:#"State"]];
state.tag=116;
[cell addSubview:state];
[state release];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, tableView.sectionHeaderHeight)];
sectionHeaderView.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
sectionHeaderView.userInteractionEnabled = YES;
sectionHeaderView.tag = section;
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(12,2, tableView.bounds.size.width-10, 18)];
headerLabel.backgroundColor = [UIColor whiteColor];
[headerLabel setTextAlignment:UITextAlignmentCenter];
headerLabel.textColor = [UIColor grayColor];
headerLabel.shadowColor = [UIColor darkGrayColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.text = #"Details";
[sectionHeaderView addSubview:headerLabel];
return sectionHeaderView;
}
Here i have given it to be 205 .but when the text in the tableViewCell increases then i couldn't see the last lines of the label of last tableViewCell..
How can i make tableViewCell to auto-resize its height ?
You can specify the height of a cell in – tableView:heightForRowAtIndexPath:.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height=60; //set default row height
if(check_for_the_rows_which_need_increased_height) {
height+=50; //Add appropriate increase
}
return height;
}
You need to call [yourTableView reloadData] or [yourTableView reloadRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationAutomatic] for above method to be called again.
You can just check for you textHeight and then set your row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *Yourtext = [yourTextAry objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(yourLabelWidth , 20000.0f);
CGSize size = [Yourtext sizeWithFont: [UIFont fontWithName:#"Verdana" size:13] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height;
}
and if text is null then remove it from array dear or just pass height to zero..:)

How can I search data from tableview cell?

I have an UITableview controller which representing fetched XML data. For representing these data I used five UILabel. Now I have to add a searchbar at the top of the UITableview. So programmatically I have added a searchbar.
Now I have used searching theorem for search data from the UITableview. But It is not working. I can search data from UItableview when only one text in the UItableviewcell without any UIlabel or something else but in my UItableviewcell cell are taking five UILabel that's why it's becoming tough for me to search data from the UItableviewcell. For understanding I am attaching my code how I am representing my XML data in tableview cell.
This is my XML data representation in UITableviewCell...
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:15.0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png"]];
cell.accessoryView = imageView;
cell.accessoryType = UITableViewCellSelectionStyleNone;
tableView.separatorColor = [UIColor clearColor];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
cellView = [[[UIView alloc] initWithFrame:CGRectMake(5,8,290, 120)] autorelease];
cellView.backgroundColor = [UIColor clearColor];
cellView.tag =10;
[cell.contentView addSubview:cellView];
imgView = [[UIImageView alloc] initWithFrame:CGRectMake(2, 40, 48, 48)];
imgView.image = [UIImage imageNamed:#"productbox.png"];
imgView.layer.borderColor = [UIColor blackColor].CGColor;
imgView.layer.borderWidth = 2.0;
imgView.tag = 5;
[cellView addSubview:imgView];
CGRect idLabelRect = CGRectMake(65, 0, 190, 18);
idLabel = [[[UILabel alloc] initWithFrame:idLabelRect] autorelease];
idLabel.textAlignment = UITextAlignmentLeft;
idLabel.textColor = [UIColor blackColor];
idLabel.font = [UIFont systemFontOfSize:12];
idLabel.backgroundColor = [UIColor clearColor];
idLabel.layer.borderColor = [UIColor grayColor].CGColor;
idLabel.tag = 0;
CGRect statusRect = CGRectMake(65, 22, 190, 22);
statusLabel = [[[UILabel alloc] initWithFrame:statusRect] autorelease];
statusLabel.textAlignment = UITextAlignmentLeft;
statusLabel.textColor = [UIColor blackColor];
statusLabel.font = [UIFont systemFontOfSize:12];
statusLabel.backgroundColor = [UIColor clearColor];
statusLabel.layer.borderColor = [UIColor grayColor].CGColor;
statusLabel.tag = 1;
CGRect orderDateRect = CGRectMake(65, 48, 190, 22);
orderDate = [[[UILabel alloc] initWithFrame:orderDateRect] autorelease];
orderDate.textAlignment = UITextAlignmentLeft;
orderDate.textColor = [UIColor blackColor];
orderDate.font = [UIFont systemFontOfSize:12];
orderDate.backgroundColor = [UIColor clearColor];
orderDate.layer.borderColor = [UIColor grayColor].CGColor;
orderDate.tag = 2;
CGRect byRect = CGRectMake(65, 75, 190, 22);
byLabel = [[[UILabel alloc] initWithFrame:byRect] autorelease];
byLabel.textAlignment = UITextAlignmentLeft;
byLabel.textColor = [UIColor blackColor];
byLabel.font = [UIFont systemFontOfSize:12];
byLabel.backgroundColor = [UIColor clearColor];
byLabel.layer.borderColor = [UIColor grayColor].CGColor;
byLabel.tag = 3;
CGRect totalRect = CGRectMake(65, 98, 190, 22);
totalLabel = [[[UILabel alloc] initWithFrame:totalRect] autorelease];
totalLabel.textAlignment = UITextAlignmentLeft;
totalLabel.textColor = [UIColor blackColor];
totalLabel.font = [UIFont systemFontOfSize:12];
totalLabel.backgroundColor = [UIColor clearColor];
totalLabel.layer.borderColor = [UIColor grayColor].CGColor;
totalLabel.tag = 4;
[cellView addSubview:idLabel];
[cellView addSubview:statusLabel];
[cellView addSubview:orderDate];
[cellView addSubview:byLabel];
[cellView addSubview:totalLabel];
}
if(searching == YES){
//[cell setText:[tableData objectAtIndex:indexPath.row]];
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
}
else{
cellView = (UIView *)[cell.contentView viewWithTag:10];
idLabel = (UILabel *)[cellView viewWithTag:0];
statusLabel = (UILabel *)[cellView viewWithTag:1];
orderDate = (UILabel *)[cellView viewWithTag:2];
byLabel = (UILabel *)[cellView viewWithTag:3];
totalLabel = (UILabel *)[cellView viewWithTag:4];
imgView = (UIImageView *)[cellView viewWithTag:5];
if(pendingOrder == NO && todaysOrder == NO){
idLabel.text = [NSString stringWithFormat:#"Order Id: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:0]];
statusLabel.text = [NSString stringWithFormat:#"Status: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:1]];
orderDate.text = [NSString stringWithFormat:#"Date: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:2]];
byLabel.text =[NSString stringWithFormat:#"By: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:3]];
totalLabel.text =[NSString stringWithFormat:#"Total: %#",[[records objectAtIndex:indexPath.section] objectAtIndex:4]];
}
else if(pendingOrder == YES && todaysOrder == NO){
idLabel.text = [NSString stringWithFormat:#"Order Id: %#",[[pendingRecords objectAtIndex:indexPath.section] objectAtIndex:0]];
statusLabel.text = [NSString stringWithFormat:#"Status: %#",[[pendingRecords objectAtIndex:indexPath.section] objectAtIndex:1]];
orderDate.text = [NSString stringWithFormat:#"Date: %#",[[pendingRecords objectAtIndex:indexPath.section] objectAtIndex:2]];
byLabel.text =[NSString stringWithFormat:#"By: %#",[[pendingRecords objectAtIndex:indexPath.section] objectAtIndex:3]];
totalLabel.text =[NSString stringWithFormat:#"Total: %#",[[pendingRecords objectAtIndex:indexPath.section] objectAtIndex:4]];
}
}
return cell;
}
And this searching Delegate.....
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
searching = YES;
// only show the status bar’s cancel button while in edit mode
sBar.showsCancelButton = YES;
sBar.autocorrectionType = UITextAutocorrectionTypeNo;
// flush the previous search content
[tableData removeAllObjects];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
searching = NO;
sBar.showsCancelButton = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
[tableData removeAllObjects];// remove all data that belongs to previous search
if([searchText isEqualToString:#""] && searchText==nil){
[tableview reloadData];
return;
}
NSInteger counter = 0;
for(NSString *name in dataSource)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSRange r = [name rangeOfString:searchText];
if(r.location != NSNotFound)
{
if(r.location== 0)//that is we are checking only the start of the names.
{
[tableData addObject:name];
}
}
counter++;
[pool release];
}
[tableview reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
// if a valid search was entered but the user wanted to cancel, bring back the main list content
[tableData removeAllObjects];
searching = NO;
[tableData addObjectsFromArray:dataSource];
#try{
searching = NO;
[tableview reloadData];
}
#catch(NSException *e){
}
[sBar resignFirstResponder];
sBar.text = #"";
}
// called when Search (in our case “Done”) button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[sBar resignFirstResponder];
}
For more help to understand i am also attaching my viewDidLoad....
//Add the search bar
sBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,0,320,50)];
sBar.delegate = self;
searching = NO;
[self.view addSubview:sBar];
tableview.dataSource = self;
tableview.delegate = self;
//initialize the two arrays; datasource will be initialized and populated by appDelegate
searchData = [[NSMutableArray alloc] init];
tableData = [[NSMutableArray alloc] init];
[tableData addObjectsFromArray:dataSource];//on launch it should display all the records
Edit
This is my edited portion of numberOfSectionsInTableView and numberOfRowsInSection...
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if(searching == YES){
searching = NO;
sectionCount = [tableData count];
}
else {
if(pendingOrder == NO && todaysOrder == NO){
sectionCount = [records count];
NSLog(#"section cout: %d",sectionCount);
}
else if(pendingOrder == YES && todaysOrder == NO){
//Total pending order counting
sectionCount = [pendingRecords count];
NSLog(#"section cout for pending: %d",sectionCount);
}
else if(pendingOrder == NO && todaysOrder == YES){
NSLog(#"todays order number counting");
}
}
return sectionCount;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
Try this:-
copylistofItem is NSMutableArray copying data that matched with search bar criteria.
- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
int i=0
for (NSString *str in [records objectAtIndex:indexPath.i] objectAtIndex:0])
{
[searchArray addObject:str];
i++;
}
for (NSString *sTemp in searchArray)
{
NSString *txtToSearch =[[NSString alloc] initWithString:[sTemp substringWithRange:NSMakeRange(0,[searchText length])]];
if([[txtToSearch lowercaseString] isEqualToString:[searchText lowercaseString]])
{
[copyListOfItems addObject:sTemp];
}
}
[searchArray release];
searchArray = nil;
}
Also we want to know what you have written in your numberOfSections tableView Delegate.

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.