2013-03-15 102 views
-1

我有一個表格視圖,每行都有一個添加按鈕,點擊下面添加的新行我想要點擊該行中有一個文本字段,用戶可以輸入一個texh它可以幫助我如何添加行中的文本字段。 這裏是我的代碼在表格視圖上添加文本字段按鈕點擊

-(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]; 

} 

和表視圖方法

- (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; 
} 
+0

哦確定這是不是我的答案我猜... – Priyanka 2013-03-15 06:22:45

+0

一旦與破發點,如果檢查( indexPath.row ==([_choices count])&& self.editing)條件是否滿足。 – Balu 2013-03-15 06:27:14

+0

這個值呢([_choices count])&& self.editing)? – Balu 2013-03-15 06:34:02

回答

0

嘗試像這可能是它可以幫助你,例如位置

1.take一個全局變量整數類型。 (void)buttonclicked:(UIButton *)發送方法將調用此方法,您將爲該位置分配btn.tag值。

3.after您重裝在tableview中的cellForRowAtIndexPath方法表視圖需要一個條件像

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

我嘗試過,但無法做到 – Priyanka 2013-03-15 06:23:08