在表格视图单元格中添加子行
问题描述:
我正在制作一个项目,在该项目中,我正在制作一个待办事宜应用程序,并将其放在桌面视图中,我在其中为每个单元格上的添加按钮添加功能,并点击该按钮我想添加一个层次的子单元格点击单元格和所有单元格相同。 你能告诉我如何做到这一点?我会非常感激。在表格视图单元格中添加子行
预先感谢您。 下面是一个代码:
- (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;
}
答
在按钮的操作添加以下代码
[yourTableViewObject beginUpdates];
在didSelectRowAtIndexPath方法,得到待插入的索引。
NSIndexPath *insertPath = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
在IndexPath插入该行: -
[yourTableViewObject insertRowsAtIndexPaths:[NSArray arrayWithObject:insertPath] withRowAnimation:UITableViewRowAnimationTop];
[yourTableViewObject endUpdates];
不要ü想刚才添加的单击的单元格下面的电池??因为没有什么比孩子的细胞。您可以自定义它看起来像一个。 – 2013-03-18 08:29:07
是的,我想在层次结构中添加单元格。 – Priyanka 2013-03-18 09:36:16
得到了解决方案? – 2013-03-18 11:35:52