-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;
}
哦確定這是不是我的答案我猜... – Priyanka 2013-03-15 06:22:45
一旦與破發點,如果檢查( indexPath.row ==([_choices count])&& self.editing)條件是否滿足。 – Balu 2013-03-15 06:27:14
這個值呢([_choices count])&& self.editing)? – Balu 2013-03-15 06:34:02