我有一个类似...的桌面视图。
细胞xxxx,yyyy& zzzz是固定的,因此没有对它们进行点击操作。但单元格“显示”是可点击的。我想在点击时在“显示”单元格下显示六个单元格。
因此,在单击“显示”单元格后,该表格将显示为..
在这里,我所做的是,
将cell.textLabel.text
更改为“显示”为“隐藏”
在[tableView reloadData];
方法中使用tableView:didSelectRowAtIndexPath:
。
我的代码是:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"My title";
// This Mutable array contains the hided cell objects
hidedCellsArray = [[NSMutableArray alloc] initWithObjects:@"Cell 1", @"Cell 2", @"Cell 3", @"Cell 4", @"Cell 5", @"Cell 6", nil];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
else if(indexPath.section == 1)
{
if (isShowClicked) //isShowClicked is a boolean value that determines whether the show cell was clicked or not
{
if (indexPath.row == 0)
{
cell.textLabel.text = @"Hide";
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.textAlignment=UITextAlignmentCenter;
}
else
{
cell.textLabel.text = [hidedCellsArray objectAtIndex:indexPath.row-1];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.textAlignment=UITextAlignmentCenter;
}
}
else
{
cell.textLabel.text = @"Show";
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.textAlignment=UITextAlignmentCenter;
}
}
...
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1)
{
if (isShowClicked)
{
isShowClicked = NO;
}
else
{
isShowClicked = YES;
}
[tableView reloadData];
}
}
我需要什么:
我想点击显示/隐藏按钮时为单元格设置动画。我知道,应该使用方法insertRowsAtIndexPaths: withRowAnimation:
来实现插入效果。但我真的没有看到任何简单的例子。我是否应该使用setEditing:animated:
或tableView: commitEditingStyle: forRowAtIndexPath:
方法等其他方法来执行此操作?
第二件事是,在动画(单元格插入)发生之前,我想将tableview移动到第2部分(即“show”单元格)到顶部。然后应该发生插入单元格的动画。因此,单击显示单元格后表格的最终外观应该是..
需要帮助..我只是困惑!!
答案 0 :(得分:2)
对于第一部分,您可以在Table View Programming Guide for iOS下查看“行和节的批量插入,删除和重新加载”部分。最重要的是您需要确保您的数据源与更改匹配。
对于第二个问题,您可以将表格视图的contentOffset
设置为第二个部分标题的原点。类似的东西:
self.tableView.contentOffset = CGPointMake(0, [self.tableView rectForSection:1].origin.y);
如果您想使用动画来获得更好的UE,请执行
[UIView animateWithDuration:duration animations:^{
//Move table view to where you want
} completion:^(BOOL finished){
//insert rows when table view movement animation finish
}];
答案 1 :(得分:0)
尝试使用 UIView CommitAnimation
答案 2 :(得分:0)
不要挑剔但是“hidedArray”应该是“hiddenArray”,因为语法正确。 无论如何,我认为您不需要为隐藏项目单独的数组。例如,假设您使用“dataArray”来填充tableview。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
if ((indexpath.section == 1) && (indexpath.row == 0)) {
if (isShowClicked) {
cell.textLabel.text = @"Hide";
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.textAlignment=UITextAlignmentCenter;
} else {
cell.textLabel.text = @"Show";
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.textAlignment=UITextAlignmentCenter;
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ((indexpath.section == 1) && (indexpath.row == 0) && (isShowClicked == NO)) {
isShowClicked = YES;
[self.tableView beginUpdates];
[dataArray addObjectsFromArray:[NSArray arrayWithObjects:@"cell1",@"cell2",@"cell3",@"cell4",@"cell5",@"cell6", nil]];
NSArray *paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[dataArray count]-1 inSection:1]];
//the first section is section 0, so this is actually the second one
[[self tableView] insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
showIsClicked = YES;
self.tableview.contentOffset = xy;
//the value of the tableView's contentOffset when it is scrolled to the right position. You can get it by NSLog-ing the contentOffset property and scrolling to the desired position
} else if ((indexpath.section == 1) && (indexpath.row == 0) && (isShowClicked == YES)) {
isShowClicked = NO;
[self.tableView beginUpdates];
[array removeObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(indexpath.row + 1, 6)]];
[tableView endUpdates];
self.tableview.contentOffset = xy;
//the value of the tableView's contentOffset when it is scrolled to the right position. You can get it by NSLog-ing the contentOffset property and scrolling to the desired position
}
}
可能会有一些错误,但我认为这是最正确的。