UITableView Scrolling slowness issues when loading cell - iphone

I am having issues with the speed in which my tableview Cells scroll. You can see that it hangs up when dequeuing and reusing a cell. Below is my code that I use to create the cells. I am using 2 custom cells and one cell is if there is a image and the other cell is if the user didn't attach an image. Any insight would be greatly appreciated. I should also add that I wanted to add a space between the cells so basically I create a new section containing a single cell and that is why you will always see in my code indexPath.section.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *imageCell = #"ShameImage";
static NSString *noImageCell = #"ShameNoImageCell";
static NSString *noCell = #"NoCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:noCell];
if ([[blobID objectAtIndex:indexPath.section] isEqualToNumber:[NSNumber numberWithInt:1]])
{
ShameViewCell *cell = (ShameViewCell *)[self.tableView dequeueReusableCellWithIdentifier:imageCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameImageCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
CALayer *cellImageView = [cell.imageView layer];
[cellImageView setMasksToBounds:YES];
[cellImageView setCornerRadius:10.0];
IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
[iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
iiv.tag = 999;
[iiv loadImageFromURL];
[cell.imageView addSubview:iiv];
cell.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapImage = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(openPicture:)];
tapImage.numberOfTapsRequired = 1;
[cell.imageView addGestureRecognizer:tapImage];
[cell.contentView.layer setCornerRadius:10];
[cell.contentView setBackgroundColor:[UIColor blackColor]];
[cell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
cell.lastShame.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[cell.lastShame setTextColor:insideColor];
[cell.lastShame setFont:[UIFont fontWithName:text_font_name size:14]];
cell.lastShame.text = [userShame objectAtIndex:indexPath.section];
[cell.shameDate setTextColor:insideColor];
[cell.shameDate setFont:[UIFont fontWithName:text_font_name size:11]];
cell.shameDate.text = [createDt objectAtIndex:indexPath.section];
[cell.userLabel setTextColor:insideColor];
[cell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
cell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
[cell.lastShame setBackgroundColor:[UIColor clearColor]];
return cell;
}
}else
{
ShameNoImage *cell = (ShameNoImage *)[self.tableView dequeueReusableCellWithIdentifier:noImageCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameNoImageCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
CALayer *cellImageView = [cell.imageView layer];
[cellImageView setMasksToBounds:YES];
[cellImageView setCornerRadius:10.0];
[cellImageView setBorderWidth:1.0];
[cellImageView setBorderColor:[[UIColor whiteColor] CGColor]];
[cell.contentView.layer setCornerRadius:10];
[cell.contentView setBackgroundColor:[UIColor blackColor]];
[cell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
cell.shameLabel.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[cell.shameLabel setTextColor:insideColor];
[cell.shameLabel setFont:[UIFont fontWithName:text_font_name size:14]];
cell.shameLabel.text = [userShame objectAtIndex:indexPath.section];
[cell.dateLabel setTextColor:insideColor];
[cell.dateLabel setFont:[UIFont fontWithName:text_font_name size:11]];
cell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
[cell.userLabel setTextColor:insideColor];
[cell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
cell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
[cell.shameLabel setBackgroundColor:[UIColor clearColor]];
return cell;
}
}
return cell;
}

The first thing that jumps out are the calls to setCornerRadius. A quick test would be to cut out that corner radius stuff to see if that helps your speed. If that's you're problem you'll have to switch to drawing with CG and a UIBezierPath.
The next thing to look at would be transparency and the number of subviews you are using. It's hard to tell with what is posted above but the more subviews, especially with alpha are going to mean more work compositing. But that can be hard to get around depending on your design but drawing with CG or using images can help.

I figured it out. I was recreating and drawing every cell. I needed to set the reuseIdentifier in the Cell's Subclass and then change the building the the table to be like this.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *imageCell = #"ShameImage";
static NSString *noImageCell = #"ShameNoImageCell";
ShameViewCell *iCell = (ShameViewCell *)[self.tableView dequeueReusableCellWithIdentifier:imageCell];
ShameNoImage *niCell = (ShameNoImage *)[self.tableView dequeueReusableCellWithIdentifier:noImageCell];
if ([[blobID objectAtIndex:indexPath.section] isEqualToNumber:[NSNumber numberWithInt:1]])
{
if (iCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameImageCell" owner:self options:nil];
iCell = [nib objectAtIndex:0];
CALayer *cellImageView = [iCell.imageView layer];
[cellImageView setMasksToBounds:YES];
IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
[iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
iiv.tag = 999;
[iiv loadImageFromURL];
[iCell.imageView addSubview:iiv];
iCell.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapImage = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(openPicture:)];
tapImage.numberOfTapsRequired = 1;
[iCell.imageView addGestureRecognizer:tapImage];
[iCell.contentView setBackgroundColor:[UIColor blackColor]];
[iCell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
iCell.lastShame.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[iCell.lastShame setTextColor:insideColor];
[iCell.lastShame setFont:[UIFont fontWithName:text_font_name size:14]];
iCell.lastShame.text = [userShame objectAtIndex:indexPath.section];
[iCell.shameDate setTextColor:insideColor];
[iCell.shameDate setFont:[UIFont fontWithName:text_font_name size:11]];
iCell.shameDate.text = [createDt objectAtIndex:indexPath.section];
[iCell.userLabel setTextColor:insideColor];
[iCell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
iCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return iCell;
}else
{
[[iCell.imageView viewWithTag:999] removeFromSuperview];
IndicatorImageView *iiv = [[IndicatorImageView alloc] initWithFrame:CGRectMake(0, 0, 88, 88)];
[iiv setShameID:[[shameID objectAtIndex:indexPath.section] stringValue]];
iiv.tag = 999;
[iiv loadImageFromURL];
[iCell.imageView addSubview:iiv];
iCell.lastShame.text = [userShame objectAtIndex:indexPath.section];
iCell.shameDate.text = [createDt objectAtIndex:indexPath.section];
iCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return iCell;
}
}else
{
if (niCell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ShameNoImageCell" owner:self options:nil];
niCell = [nib objectAtIndex:0];
[niCell.contentView setBackgroundColor:[UIColor blackColor]];
[niCell.contentView setAlpha:0.7f];
UIColor *insideColor = [UIColor colorWithRed:color_red green:color_green blue:color_blue alpha:1];
niCell.shameLabel.contentInset = UIEdgeInsetsMake(-11, -8, 0, 0);
[niCell.shameLabel setTextColor:insideColor];
[niCell.shameLabel setFont:[UIFont fontWithName:text_font_name size:14]];
niCell.shameLabel.text = [userShame objectAtIndex:indexPath.section];
[niCell.dateLabel setTextColor:insideColor];
[niCell.dateLabel setFont:[UIFont fontWithName:text_font_name size:11]];
niCell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
[niCell.userLabel setTextColor:insideColor];
[niCell.userLabel setFont:[UIFont fontWithName:text_font_name size:11]];
niCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return niCell;
}else
{
niCell.shameLabel.text = [userShame objectAtIndex:indexPath.section];
niCell.dateLabel.text = [createDt objectAtIndex:indexPath.section];
niCell.userLabel.text = [#"Post By: " stringByAppendingString:[userName objectAtIndex:indexPath.section]];
return niCell;
}
}
return niCell;
}

Related

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

UILabel in UITableViewCell not drawing until cell is at top of screen

I have strange problem with UILabel in a UITableViewCell. It does not appear until I pull the screen down so that the UITableViewCell is at the top. Here is a video of the problem.
http://youtu.be/apTJd1Y4RZk
HEre is the code when the cell is created.
TVC_Location_View_Text *cell = (TVC_Location_View_Text *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TVC_Location_View_Text" owner:nil options: nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = ((TVC_Location_View_Text *) currentObject);
break;
}
}
}
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:15.0];
CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);
CGSize labelSize = [self.sLocationText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
CGRect rect = CGRectMake(10, 10, labelSize.width, labelSize.height);
UILabel* labelText = [[UILabel alloc] initWithFrame:rect];
[labelText setFont:cellFont];
[labelText setLineBreakMode:UILineBreakModeWordWrap];
[labelText setNumberOfLines:0];
[labelText setBackgroundColor:[UIColor clearColor]];
[labelText setTextColor:appDelegate.colorDarkText];
[cell addSubview:labelText];
[labelText setText:self.sLocationText];
return cell;
Creation of the UILabel should be inside the creation of the cell itself
This is an issue, i dont know if it will resolve your bug, but it still is a bug,
Please change your code to
TVC_Location_View_Text *cell = (TVC_Location_View_Text *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel* labelText;
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TVC_Location_View_Text" owner:nil options: nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = ((TVC_Location_View_Text *) currentObject);
break;
}
}
labelText = [[UILabel alloc] initWithFrame:rect];
[labelText setFont:cellFont];
[labelText setLineBreakMode:UILineBreakModeWordWrap];
[labelText setNumberOfLines:0];
[labelText setBackgroundColor:[UIColor clearColor]];
[labelText setTextColor:appDelegate.colorDarkText];
labelText.tag = 111;
[cell addSubview:labelText];
}
labelText = (UILabel*)[cell viewWithTag:111];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:15.0];
CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);
CGSize labelSize = [self.sLocationText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
CGRect rect = CGRectMake(10, 10, labelSize.width, labelSize.height);
[labelText setText:self.sLocationText];
return cell;
Try this code, and see if it works out
You should use -[UITableViewDelegate willDisplayCell:forRowAtIndexPath:] delegate method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TVC_Location_View_Text *cell = (TVC_Location_View_Text *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel* labelText;
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TVC_Location_View_Text" owner:nil options: nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = ((TVC_Location_View_Text *) currentObject);
break;
}
}
labelText = [[UILabel alloc] initWithFrame:rect];
[labelText setFont:cellFont];
[labelText setLineBreakMode:UILineBreakModeWordWrap];
[labelText setNumberOfLines:0];
[labelText setBackgroundColor:[UIColor clearColor]];
[labelText setTextColor:appDelegate.colorDarkText];
labelText.tag = 111;
[cell addSubview:labelText];
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
UILabel* labelText = (UILabel*)[cell viewWithTag:111];
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:15.0];
CGSize constraintSize = CGSizeMake(300.0f, MAXFLOAT);
CGSize labelSize = [self.sLocationText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
CGRect rect = CGRectMake(10, 10, labelSize.width, labelSize.height);
[labelText setText:self.sLocationText];
}

tableview refreshing while searching on iphone

I have a table view that shows 3 data side by side in table ,lets say A(name) , B(roll no) , C(id).
Now if user do search based on A's (search by name) (table get refreshed with name while searching only) value I want to either hide the content of B & C or they should show the correct value while search is on , not the old value which they got before searching.
I couldn't find the solution how to deal with remaining B & C.kindly suggest
here is my code
- (void) searchTableView
{
NSLog(#"search button is down %#",searchBar.text);
NSString *searchText = searchBar.text;
NSMutableArray *searchArray = [[NSMutableArray alloc] init];
if(SearchQuery==1)
{
[searchArray addObjectsFromArray:[entityArray valueForKey:#"Name"]];
}
if(SearchQuery==2)
{
[searchArray addObjectsFromArray:[entityArray valueForKey:#"IC"]];
}
for (NSString *sTemp in searchArray)
{
NSLog(#"copyListOfItems in search -%#",copyListOfItems);//final array for names
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[copyListOfItems addObject:sTemp];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = (NSManagedObject *)[entityArray objectAtIndex:indexPath.row];
NSString *CellIdentifier = #"Cell";
int indicator = UITableViewCellAccessoryNone;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 50.0)] autorelease];//220
mainLabel.tag = MAINLABEL_TAG;
[mainLabel setFont:[UIFont fontWithName:#"Helvetica-Bold" size:18]];//Palatino-BoldItalic
mainLabel.textAlignment = UITextAlignmentLeft;
mainLabel.textColor = [UIColor blackColor];
secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(313.0, 9.0, 200.0, 25.0)] autorelease];//83
secondLabel.tag = SECONDLABEL_TAG;
smallSystemFontSize]]];
[secondLabel setFont:[UIFont fontWithName:#"Helvetica" size:18]];
secondLabel.textAlignment = UITextAlignmentLeft;
secondLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:secondLabel];
//// 3rd labl display
thirdLabel = [[[UILabel alloc] initWithFrame:CGRectMake(560.0, 9.0, 250.0, 25.0)] autorelease];//83
thirdLabel.tag = THIRDLABEL_TAG;
//thirdLabel.font = [UIFont systemFontOfSize:13.0];
[thirdLabel setFont:[UIFont fontWithName:#"Helvetica" size:18]];//Palatino-BoldItalic
thirdLabel.textAlignment = UITextAlignmentLeft;
thirdLabel.textColor = [UIColor blackColor];
[cell.contentView addSubview:thirdLabel];
}
if(searching)
{
NSLog(#" seacrh List in table view =%#",[copyListOfItems objectAtIndex:indexPath.row]);
// NSString *s=[object valueForKey:#"Name"];
NSLog(#"copyListOfItems length ---------%d",[copyListOfItems count]);
cell.textLabel.text =[copyListOfItems objectAtIndex:indexPath.row] ;
if(SearchQuery==2)
{
secondLabel.text=#"kk";// not working
secondLabel.alpha=1; // this is not working i cant upadate
}
}
else
{
if(self.entityName == #"Parent")
{
{
indicator = UITableViewCellAccessoryDisclosureIndicator;
CellIdentifier = #"CellWithDisclosure";
}
}
if(SearchQuery==2)
{
cell.textLabel.text =[object valueForKey:#"IC"] ;
secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
thirdLabel = (UILabel *)[cell.contentView viewWithTag:THIRDLABEL_TAG];
NSLog(#"sex is %# ",[object valueForKey:#"Name"]);
secondLabel.text=[object valueForKey:#"Name"] ;
secondLabel.alpha=0;
thirdLabel.text=[object valueForKey:#"roll"] ;
}

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.