简体   繁体   English

当其中一个单元格被选中时,禁用其他表格视图单元格的文本字段

[英]Disable textfield for other table view cells, when one of the cell is selected

I have a textField on custom cell in a tableView . 我在tableView自定义单元格上有一个textField When one of the cell is selected I am able to disable textField for that particular cell by using set selected method. 当选择其中一个cell时,我可以使用set selected方法禁用该特定celltextField

(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. 要求是禁用textField的中的任何一个时用于其它细胞cell被选择。 Please guide me on this, Thanks. 请指导我,谢谢。

You have to use instance variable, you could use indexPath. 您必须使用实例变量,您可以使用indexPath。

When user select any cell, set value of that instance variable to selected indexPath. 当用户选择任何单元格时,将该实例变量的值设置为选定的indexPath。

And reloadTable. 和reloadTable。

In cellForRowAtIndexPath check if current indexPath is selected indexPath, if yes enable textField else disable it. cellForRowAtIndexPath检查当前indexPath是否被选中indexPath,如果是,则启用textField,否则禁用它。

Create button on tableview cell and enable or disable textfield on tableview or can apply this logic on tableview didSelectRowAtIndexPath 在tableview单元格上创建按钮并在tableview上启用或禁用textfield,或者可以在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; 
}

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 假设你有一个Class对象, DataClass创建一个BOOL属性,比如说textFieldSelected ,现在按照步骤操作

- (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; 最初你可以设置textFieldSelected=YES; so that all the textFields are enabled. 以便启用所有textField。 You can wish to play with object and it will work that way. 你可以希望玩对象,它将以这种方式工作。 All the best. 祝一切顺利。

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. 单元格选择计数增加将在索引路径方法中选择行并在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. 在didDeselectRowAtIndexPath方法中,单元格选择计数减少,并在NsuserDefaults中设置该值。

selectedIndex--; selectedIndex--;

In my custom table view cell class in textFieldDidBeginEditing method get the stored value from NsUserDefaults and act upon. 在我的自定义表视图中,textFieldDidBeginEditing方法中的单元格类从NsUserDefaults获取存储值并进行操作。

NSString *SelectedIndexString = [[NSUserDefaults standardUserDefaults] stringForKey:@"SelectedIndexString"]; 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. 谢谢你的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM