代码之家  ›  专栏  ›  技术社区  ›  casillas

获取customtableview文本字段的indexPath

  •  0
  • casillas  · 技术社区  · 8 年前

    MyCustomCell ,并在 cellForRowIndexPath tableview委托。当用户更改textfield值并调用 textFieldShouldEndEditing

    然而,我想知道如何知道哪个textfield(indexPath)被更改了?

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIdentifier = @"Cell";
    
        MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if (cell == nil) 
        {  
            // use this if you created your cell with IB
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil] objectAtIndex:0];   
    
            // otherwise use this  
            cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    
    
            // now set the view controller as the text field delegate  
            cell.textField.delegate = self;
        }
    
        // configure cell...
    
        return cell;
    }
    
    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
    {
        [textField resignFirstResponder];
        return YES;
    }
    
    3 回复  |  直到 8 年前
        1
  •  2
  •   Bilal hao zou    8 年前

    设置 UITextField cellForRowAtIndexPath 方法如果tableview中只有一个部分,则可以将该行设置为标记。这样地。

    cell.textField.delegate = self;
    cell.textField.tag = indexPath.row;
    

    在你的 textFieldShouldEndEditing

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
    {
        int row = textField.tag;
    
        [textField resignFirstResponder];
        return YES;
    }
    
        2
  •  2
  •   成璐飞    8 年前

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { if ([[[textField superview] superview] isKindOfClass:[MyCustomCell class]]) { MyCustomCell *cell = (MyCustomCell *)[[textField superview] superview]; NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; // this is what you want. } return YES; }

        3
  •  1
  •   Shabbir Ahmad    8 年前

    你可以试试这个:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIdentifier = @"Cell";
    
        MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        if (cell == nil) 
        {  
            // use this if you created your cell with IB
            cell = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil] objectAtIndex:0];   
    
            // otherwise use this  
            cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    
    
            // now set the view controller as the text field delegate  
            cell.textField.delegate = self;
            textField.tag = indexPath.Row;
        }
    
        // configure cell...
    
        return cell;
    }
    
    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
    {
        NSLog("Selected Cell is:%@",textField.tag);
        [textField resignFirstResponder];
        return YES;
    }
    
    推荐文章