gpt4 book ai didi

ios - 定制的 tableView à la Tweetbot

转载 作者:技术小花猫 更新时间:2023-10-29 10:58:11 26 4
gpt4 key购买 nike

您在下面看到的是 Tweetbot iPhone application 的屏幕截图.我想问的是,如果您知道我可以实现 Tapbots 人员在他们的 tableView 中实现的目标的方法:您点击前 tableView 的一个单元格,它会显示一组操作,再次点击它就会消失。

enter image description here

这是一个非常酷的实现,我很想知道您对我如何做到这一点的看法,这既是为了练习,也是因为我希望在未来的项目中使用它的变体。

有什么想法吗?

最佳答案

当用户选择单元格时,保存所选单元格的索引路径并在其下方添加一行。如果用户选择相同的销售隐藏它下面的行。如果用户选择另一个单元格,则隐藏当前选定的单元格并在新选定的单元格下方显示一个新单元格。

@interface RCTableViewController ()

@property (nonatomic, strong) NSMutableArray *tableViewData;
@property (nonatomic, strong) NSIndexPath *selectedIndexPath;

@end

@implementation RCTableViewController

@synthesize tableViewData = _tableViewData;
@synthesize selectedIndexPath = _selectedIndexPath;

- (void)viewDidLoad
{
[super viewDidLoad];

self.tableViewData = [NSMutableArray arrayWithObjects:
@"Cell 0",
@"Cell 1",
@"Cell 2",
@"Cell 3",
@"Cell 4",
@"Cell 5",
@"Cell 6",
@"Cell 7",
@"Cell 8",
nil];
self.selectedIndexPath = nil;
[self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tableViewData.count;
}

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

if (indexPath == self.selectedIndexPath) {
static NSString *DropDownCellIdentifier = @"DropDownCell";

cell = [tableView dequeueReusableCellWithIdentifier:DropDownCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DropDownCellIdentifier];
}

cell.textLabel.text = @"Drop Down Cell";

} else {
static NSString *DataCellIdentifier = @"DataCell";

cell = [tableView dequeueReusableCellWithIdentifier:DataCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DataCellIdentifier];
}

cell.textLabel.text = [self.tableViewData objectAtIndex:indexPath.row];
}

return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *dropDownCellIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1
inSection:indexPath.section];

if (!self.selectedIndexPath) {
// Show Drop Down Cell
[self.tableViewData insertObject:@"Drop Down Cell" atIndex:dropDownCellIndexPath.row];

[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath]
withRowAnimation:UITableViewRowAnimationTop];

self.selectedIndexPath = indexPath;
} else {
NSIndexPath *currentdropDownCellIndexPath = [NSIndexPath indexPathForRow:self.selectedIndexPath.row + 1
inSection:self.selectedIndexPath.section];

if (indexPath.row == self.selectedIndexPath.row) {
// Hide Drop Down Cell
[self.tableViewData removeObjectAtIndex:currentdropDownCellIndexPath.row];

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentdropDownCellIndexPath]
withRowAnimation:UITableViewRowAnimationTop];

self.selectedIndexPath = nil;
} else if (indexPath.row == currentdropDownCellIndexPath.row) {
// Dropdown Cell Selected - No Action
return;
} else {
// Switch Dropdown Cell Location
[tableView beginUpdates];
// Hide Dropdown Cell
[self.tableViewData removeObjectAtIndex:currentdropDownCellIndexPath.row];

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:currentdropDownCellIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];

// Show Dropdown Cell
NSInteger dropDownCellRow = indexPath.row + ((indexPath.row >= currentdropDownCellIndexPath.row) ? 0 : 1);
dropDownCellIndexPath = [NSIndexPath indexPathForRow:dropDownCellRow
inSection:indexPath.section];


[self.tableViewData insertObject:@"Drop Down Cell" atIndex:dropDownCellIndexPath.row];

[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:dropDownCellIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];

self.selectedIndexPath = [NSIndexPath indexPathForRow:dropDownCellIndexPath.row - 1
inSection:dropDownCellIndexPath.section];
[tableView endUpdates];
}
}
}

@end

关于ios - 定制的 tableView à la Tweetbot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9227359/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com