How to capitalized first letter of Label - iphone

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

Related

Overlapping Table cells-content [closed]

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

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

How to change the content of the UITableView with a button click?

I have a UItableview with array of contents in it.There are two array ,I want to switch the array by using a button click.
My code is
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
tab.backgroundColor = [UIColor clearColor];
tab.separatorColor = [UIColor clearColor];
cell.chapterAndVerse.text = [NSString stringWithFormat:#"%d",indexPath.row+1];
cell.chapterAndVerse.font = [UIFont fontWithName:#"Georgia" size:17.0];
cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
cell.textLabel.text = [NSString stringWithFormat:#" %#",[delegate.allSelectedVerseMalayalam objectAtIndex:indexPath.row]];
cell.textLabel.font = [UIFont fontWithName:#"testfont" size:18];
cell.backgroundColor = [UIColor clearColor];
}
my array is delegate.allSelectedVerseMalayalam ,I want this array to be changed with delegate.allSelectedVerseEnglish in button click.When the button for english is clicked by the user it changes the tableviewcontent(my table names is tab)to delegate.allSelectedVerseEnglish .How is this possible.Plese help me to figure out from this problem.
Thanks in advance.
EDIT:
- (void)viewDidLoad {
NSLog(#"%#", delegate.allSelectedVerseMalayalam);
tempArray = [[NSMutableArray alloc] init];
[tempArray addObjectsFromArray:delegate.allSelectedVerseMalayalam];
NSLog(#"%#", tempArray);
[self.tab reloadData];
}
-(IBAction)_clickbtndefaultlnguageset:(id)sender
{
[tempArray removeAllObjects];
[tempArray addObjectsFromArray:delegate.allSelectedVerseHindi];
[self.tab reloadData];
}
in tableview
cell.chapterAndVerse.text = [NSString stringWithFormat:#"%d",indexPath.row+1];
cell.chapterAndVerse.font = [UIFont fontWithName:#"Georgia" size:17.0];
cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
cell.textLabel.text = [NSString stringWithFormat:#" %#",[tempArray objectAtIndex:indexPath.row]];
// cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.font = [UIFont fontWithName:#"testfont" size:18];
cell.backgroundColor = [UIColor clearColor];
do one thing take one extra array and allocated it
define in your source file .h file
NSMutableArray *tempArray;
-(void)viewDidLoad{
//NSLog(#"%#", your Array);
tempArray = [[NSMutableArray alloc] init];
[tempArray addObjectsFromArray:yourfirstarray];
NSLog(#"%#", tempArray);
[self.tableview reloadData];
}
after prees the button
-(void)pressedBtn{
//NSLog(#"%#", your Array);
[tempArray removeAllObjects];
[tempArray addObjectsFromArray:yoursecondArray];
[self.tableView reloadData];
}
use this temp array into table array

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.