1

I have a textField on custom cell in a tableView. When one of the cell is selected I am able to disable textField for that particular cell by using set selected method.

(void)setSelected:(BOOL)selected animated:(BOOL)animated { 
   [super setSelected:selected animated:animated]; 
if (selected) {
   self.accountNameTxtField.enabled = NO; 
} else { 
   self.accountNameTxtField.enabled = YES; 
   } 
}

The requirement is to disable textField for other cells when any one of the cell is selected. Please guide me on this, Thanks.

Sanket_B
  • 735
  • 9
  • 26
Sadik
  • 91
  • 6
  • first disable all textfield than enable particular textfield so this way is easy – Jigar Jan 10 '17 at 07:16
  • - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; [cell.textField becomeFirstResponder]; } – Jigar Jan 10 '17 at 07:16
  • try this code for enable particular textfield – Jigar Jan 10 '17 at 07:17
  • Hi there, Do you want me to comment the above code in custom cell and try this in table view? – Sadik Jan 10 '17 at 07:40
  • 1
    Do you want all your `UITextField` disabled at the beginning and should only be enabled once user clicks the cell? – Rikh Jan 10 '17 at 07:48
  • When cell is seleted, i have a delete operation on tool bar at that time i shoud disable text field- i have achieved this with above code. Now when one cell is selected that text field of that selected cell is disabled. i want to disable other text fields as well – Sadik Jan 10 '17 at 08:12
  • So, you mean the cell which is selected should have TextField enable and other must be disable. Is it? – Amanpreet Jan 10 '17 at 10:20

4 Answers4

0

You have to use instance variable, you could use indexPath.

When user select any cell, set value of that instance variable to selected indexPath.

And reloadTable.

In cellForRowAtIndexPath check if current indexPath is selected indexPath, if yes enable textField else disable it.

Mohammad Zaid Pathan
  • 16,304
  • 7
  • 99
  • 130
0

Create button on tableview cell and enable or disable textfield on tableview or can apply this logic on tableview didSelectRowAtIndexPath

-(void) btnClick:(id)sender
{
 UIButton *btn = (UIButton *)sender;
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:tblView];
NSIndexPath *indexPath = [tblView indexPathForRowAtPoint:buttonPosition];
NSLog(@"table view row is ----%ld",indexPath.row);

 if (btn.isSelected == false) 
 {
    [btn setSelected:true];
   [selectRow addObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
    self.accountNameTxtField.enabled = yes;
   //selectRow is NSMutableArray
 }
  else 
 {  [btn setSelected:false];
    [selectRow removeObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
    self.accountNameTxtField.enabled = no;
 }
 }

   [tbleview relaoddata];
 if ([selectRow containsObject:[NSString  stringWithFormat:@"%ld",indexPath.row]])
 {  
 accountNameTxtField.enabled = yes; 
 }
else
{  
 accountNameTxtField.enabled = NO; 
}
Lalit kumar
  • 1,797
  • 1
  • 8
  • 14
0

Use your Data to do this, you must be having some data in for of array containing dictionaries, or class object, I am demonstrating Class Object.

Say you have a Class object DataClass create a BOOL property say textFieldSelected, now follow the steps

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath];

    DataClass *object=[self.dataArray objectAtIndex:indexPath.row];    
    [cell.textField setTag:indexPath.row];

    cell.textField.delegate=self;

    cell.textField.enabled=object.textFieldSelected;

    return cell;
}

Now when you select a row

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    for(DataClass *object in self.dataArray){
        object.textFieldSelected=NO;
    } 
    DataClass *object=[self.dataArray objectAtIndex:indexPath.row];
    object.textFieldSelected=YES;
    [tableView reloadData];

}

Initially you can set textFieldSelected=YES; so that all the textFields are enabled. You can wish to play with object and it will work that way. All the best.

iphonic
  • 12,615
  • 7
  • 60
  • 107
0

Finally i managed to find the solution with integer cell selection count.

cell selection count gets increased in will select row at index path method and set that value in NsuserDefaults.

selectedIndex++;

NSString *valueToSave = [NSString stringWithFormat:@"%ld",(long)selectedIndex];
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"SelectedIndexString"];
[[NSUserDefaults standardUserDefaults] synchronize];

cell selection count gets decreased in didDeselectRowAtIndexPath method and set that value in NsuserDefaults.

selectedIndex--;

In my custom table view cell class in textFieldDidBeginEditing method get the stored value from NsUserDefaults and act upon.

NSString *SelectedIndexString = [[NSUserDefaults standardUserDefaults] stringForKey:@"SelectedIndexString"];

NSLog(@"%ld",(long)SelectedIndexString);
NSInteger SelectedIndexCount = [SelectedIndexString integerValue];

if (SelectedIndexCount) {
    self.accountNameTxtField.enabled = NO;
}
else{
    self.accountNameTxtField.enabled = YES;

}

Thanks you so much guys. Thanks for your time.

Sadik
  • 91
  • 6