-3

[Edited Post]

For practicing, I developing a contact app without any backend. I followed only one NSMutableArray for maintaining contacts which is displayed in a UITableView.

Objective;

  • Like to search a contact (Ex: Dad), user given a name in a textfield and touch up a button
  • In the button action, check if Dad contain in the Mutable array which one i followed.
    • If present, then it definitely present in the tableView as a row. So i want to put selectionStyle for that row.
    • If search text changes means (Ex: Mom), then revert previous selectionStyle and apply to the row(Mom).
    • If not present, label notify "Not Present" as text. (Done!).

IBAction:

-(IBAction)actionSearchNameInTable:(id)sender{
if([myContactsArray containsObject:txtForContactName.text]){
myTable.tag=5;
NSInteger tempIndex=[myContactsArray indexOfObject:txtForContactName.text];    
NSIndexPath *tempIndexPath=[NSIndexPath indexPathForRow:tempIndex inSection:0];   
[self tableView:myTable didSelectRowAtIndexPath:tempIndexPath]; //broke here
NSLog(@"Call Passed!"); 
}
}

didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath  *)indexPath{
if(myTable.tag==5){
NSLog(@"called done!");
//Here i like to select that row and apply selectionStyle or favorite background color for that cell, idea?.
myTable.tag=0;
}

These are all just other part of my application. I sured that, Delegate and Datasource are connected properly.

Mohd Sadham
  • 435
  • 3
  • 19
  • 3
    You are calling "didDeselectRowAtIndexPath" and not "didSelectRowAtIndexPath". And I think you have not implemented "didDeselectRowAtIndexPath" in your class so thats the reason of crash. – Pooja M. Bohora Apr 03 '14 at 06:49
  • Where you have implemented "didDeselectRowAtIndexPath" method ? – Dharmbir Singh Apr 03 '14 at 06:49
  • Sorry to all and your time.Silly Mistake happened. @PoojaM.Bohora caught that problem first. And i like to share this [link](http://stackoverflow.com/questions/255927/didselectrowatindexpath-not-being-called) – Mohd Sadham Apr 03 '14 at 06:54

4 Answers4

2

You are calling didDeselectRowAtIndexPath and not didSelectRowAtIndexPath.

And I think you have not implemented didDeselectRowAtIndexPath in your class so thats the reason of crash.

and for your another question:

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(cell.tag==100)
    {
        cell.contentView.backgroundColor=[UIColor grayColor];

    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell=[tblviw cellForRowAtIndexPath:indexPath];
    cell.tag=100;
    cell.contentView.backgroundColor=[UIColor grayColor];
}
Pooja M. Bohora
  • 1,311
  • 1
  • 14
  • 42
0

Please try to see my edited answer...

-(IBAction)actionSearchNameInTable:(id)sender{
    if([myContactsArray containsObject:txtForContactName.text]){
    myTable.tag=5;
    NSInteger tempIndex=[myContactsArray indexOfObject:txtForContactName.text];
    NSLog(@"TEmp index:%i",tempIndex);
    NSIndexPath *tempIndexPath=[NSIndexPath indexPathForRow:tempIndex inSection:0];
    NSLog(@"Temp IP:%@",tempIndexPath);
    [self tableView:myTable didSelectRowAtIndexPath:tempIndexPath]; //Change here......
    NSLog(@"Call Passed!"); 
}
}
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

Change this:

-(IBAction)actionSearchNameInTable:(id)sender{
if([myContactsArray containsObject:txtForContactName.text]){
myTable.tag=5;
NSInteger tempIndex=[myContactsArray indexOfObject:txtForContactName.text];
NSLog(@"TEmp index:%i",tempIndex);
NSIndexPath *tempIndexPath=[NSIndexPath indexPathForRow:tempIndex inSection:0];
NSLog(@"Temp IP:%@",tempIndexPath);
[self tableView:myTable didSelectRowAtIndexPath]; //broke here
NSLog(@"Call Passed!"); 
}
}

And for custom selection background do this in your cellForRowAtIndexPath.

UIView *customColorView = [[UIView alloc] init];
customColorView.backgroundColor = [UIColor colorWithRed:180/255.0
                                                  green:138/255.0
                                                   blue:171/255.0
                                                  alpha:0.5];
cell.selectedBackgroundView =  customColorView;

Hope this helps.. :)

EDIT:

didSelectRowAtIndexPath is a delegate. It only get called when user interaction happens. I face a same problem some days ago. I did it in cellForRowAtIndexPath. For selecting a row do this:

if ([[myContactsArray objectAtIndex:indexPath.row] isEqualToString:txtForContactName.text]) {
        [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
Rashad
  • 11,057
  • 4
  • 45
  • 73
  • why i use `cellForRowAtIndexPath` and why i cant achieve in `didSelectRowAtIndexPath`. – Mohd Sadham Apr 03 '14 at 07:18
  • Your code change background when i select row manually. But, i want to select a row which is present in `txtForContactName.text`. – Mohd Sadham Apr 03 '14 at 07:45
  • Use of undeclared 'dataSource'. happened. – Mohd Sadham Apr 03 '14 at 08:04
  • dataSource is the array from where I am getting the cell data. – Rashad Apr 03 '14 at 08:07
  • From where you are getting the data. If you can't understand share cellForRowAtIndexPath code. – Rashad Apr 03 '14 at 08:09
  • - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ ....dequeue.... myTableCell.textLabel.text=[myContactsArray objectAtIndex:indexPath.row]; myTableCell.imageView.image=[myContactsPhotosArray objectAtIndex:0]; } return myTableCell; } – Mohd Sadham Apr 03 '14 at 08:17
  • Your code worked but want reloadData. Then only selectionStyle visibled. Now, where do i put reloadData. – Mohd Sadham Apr 03 '14 at 09:09
  • I could not understand what you are asking. :( – Rashad Apr 03 '14 at 09:11
  • I have one separate button action `Reload` for just reloadData of tableView. Your code gives result only after put `Reload` action. ie. result only comes if we reloadData of the tableView. – Mohd Sadham Apr 03 '14 at 09:29
  • Most possible reason is that while populating the table view for the first time txtForContactName.text may be nil or there is no match in myContactsArray. – Rashad Apr 03 '14 at 09:32
0

Action for searching that if given text is present or not:

-(IBAction)actionSearchNameInTable:(id)sender{
 [txtForContactName resignFirstResponder];
if([myContactsArray containsObject:txtForContactName.text]){
    myTable.tag=5;
    [myTable reloadData];
}
}

In cellForRowAtIndexPath:

//Better delegate to apply selection style
//Code after dequeue...
myTableCell.textLabel.text=[myContactsArray objectAtIndex:indexPath.row];
myTableCell.imageView.image=[myContactsPhotosArray objectAtIndex:0];
{
myTableCell.textLabel.text=[myContactsArray objectAtIndex:indexPath.row];
myTableCell.imageView.image=[myContactsPhotosArray objectAtIndex:0];
if(myTable.tag==5){
NSLog(@"Ready to apply selectionStyle");
if ([[myContactsArray objectAtIndex:indexPath.row] isEqualToString:txtForContactName.text]) {
[myTable selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
myTable.tag=0;           
}
}
return myTableCell;
}
Mohd Sadham
  • 435
  • 3
  • 19