-1

I have a table view and in each row there is a add button on click the new row adding below i want that onclick the row has one text field in it and a user can enter a texh on it can you help me how to add a text field in the adding row. here is my code

-(void)buttonclicked:(UIButton *)sender
{

    UIButton *btn = (UIButton *)[self.view viewWithTag:sender.tag];
    NSLog(@"clickedCell=%i", btn.tag);

    [[NSUserDefaults standardUserDefaults]setValue:[NSString stringWithFormat:@"%i", btn.tag] forKey:@"btntag"];
    [self.choices insertObject:@"newrow" atIndex:btn.tag+1];


        [self.tableView reloadData];

}

and the table view method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *CellIdentifier = @"Cell";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil)
     {


        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }



    UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
    button.tag = indexPath.row;
    button.frame = CGRectMake(280.0, 10, 25, 30.0); // x,y,width,height

    [button addTarget:self action:@selector(buttonclicked:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:button];

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:cell action:nil];
    longPress.delegate = self;
    [cell addGestureRecognizer:longPress];

    int count = 0;
    if(self.editing && indexPath.row != 0)
        count = 1;
    NSLog([NSString stringWithFormat:@"%i,%i",indexPath.row,(indexPath.row-count)]);

    // Set up the cell...
    if(indexPath.row == ([_choices count]) && self.editing)
    {
        cell.textLabel.text = @"ADD";
        return cell;
    }

    NSString *choice = [self.choices objectAtIndex:indexPath.row];

    cell.textLabel.text = choice;



    return cell;
}
3
  • oh ok this is not the i answer i guess...
    – Priyanka
    Mar 15, 2013 at 6:22
  • once check with break point if(indexPath.row == ([_choices count]) && self.editing) condition stisfied or not.
    – Balu
    Mar 15, 2013 at 6:27
  • what about this value ([_choices count]) && self.editing)?
    – Balu
    Mar 15, 2013 at 6:34

1 Answer 1

0

try like this may be it helps to you,

1.take one global variable type integer for example position.

2.when you click on particular button -(void)buttonclicked:(UIButton *)sender method will call in this method you will assign the btn.tag value to the position .

3.after that you are reloading the table view in the tableview cellForRowAtIndexPath method take one condition like

if(position==indexpath.row)
{
//add your textfield here
}.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.