ios - 隐藏或移动选定的 Cell 并在 UItableView 的 indexPath.row 中加载一个新的自定义 Cell

标签 ios objective-c iphone uitableview

我有一个 UITableView 调用 myTableView 和两个自定义 UITableViewCell 调用 TableViewCellExTableViewCell。我想要的是,当用户点击一个单元格时,现有单元格 TableViewCell 将隐藏/移动并且 ExTableViewCell 加载到该 indexpath.row当再次点击 indexpath.row 时,它会隐藏 ExTableViewCell 并在该位置恢复旧的 TableViewCell

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myArray = [[NSArray alloc] initWithObjects:@"one", @"two", @"three", @"four", @"five", @"six", nil];
    selectedIndex = -1;
}

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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (selectedIndex == indexPath.row)
    {
        return 230;
    }
    else
    {
        return 40;
    }
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    TableViewCell *Cell = (TableViewCell *)[self.myTableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!Cell)
    {
        Cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Cell.myLabel.text = [self.myArray objectAtIndex:indexPath.row];



    if (selectedIndex == indexPath.row)
    {
        static NSString *CellIdentifier = @"CellEx";
        ExTableViewCell *Cell = (ExTableViewCell *)[self.myTableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (!Cell)
        {
            Cell = [[ExTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        Cell.backgroundColor = [UIColor redColor];
        Cell.exLabel.text = [self.myArray objectAtIndex:indexPath.row];
    }
    else
    {
        // Do close cell stuff
        //Cell.backgroundColor = [UIColor clearColor];
    }

    return Cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Expand row when user taps row
    if (selectedIndex == indexPath.row)
    {
        selectedIndex = -1;
        [self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:  UITableViewRowAnimationFade];

        return;
    }

    // When user taps different row
    if (selectedIndex != -1)
    {
        NSIndexPath *prevPath = [NSIndexPath indexPathForRow:selectedIndex inSection:0];
        selectedIndex = indexPath.row;
        [self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:prevPath] withRowAnimation:UITableViewRowAnimationFade];
    }

    // When user taps new row with none expanded
    selectedIndex = indexPath.row;
    [self.myTableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}

但由于某些原因,ExTableViewCell label 没有显示任何文本。 ExTableViewCell 仍然位于其顶部。我怎样才能做到这一点?

非常感谢您的提前。祝你有美好的一天。 :)

这是输出:

enter image description here

我的问题:

enter image description here

最佳答案

您不需要“隐藏”旧单元格来显示新单元格,您只需在所需的索引路径处重新加载适当的内容。像这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    if ([self.selectedPath isEqual:indexPath]) {
        //configure the extended cell
        cell = [tableView dequeueReusableCellWithIdentifier:@"CellEx" forIndexPath:indexPath];
        ...
    } else {
        //configure the default cell
    }
}

下面是处理选中/取消选中状态的方法:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSIndexPath *oldPath = [self.selectedPath copy];
    self.selectedPath = indexPath;
    NSArray *paths = @[indexPath];
    if (oldPath && ![oldPath isEqual:indexPath]) {
        paths = [paths arrayByAddingObject:oldPath];
    } else if ([oldPath isEqual:indexPath]){
        self.selectedPath = nil;
    }
    [self.tableView beginUpdates];
    [self.tableView reloadRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
}

关于ios - 隐藏或移动选定的 Cell 并在 UItableView 的 indexPath.row 中加载一个新的自定义 Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29630406/

相关文章:

objective-c - UITableView 中分页实现的查询

iphone - 游戏中的数学逻辑(基本三角函数),这段代码在做什么?

iPhone:用户以类似于 HTML 选择的方式选择选项的最佳方式?

javascript - 如何在网站的移动设备上录制音频?

ios - 在 Swift(iOS)和 setValue forKey 中子类化 NSObject 的子类

ios - 如何确定 NSDate 是否是今天?

ios - 在具有 ARC 启用项目的仪器中的 [[NSNumberFormatter alloc] init] 中出现内存泄漏

ios - Xcode 6 现在是否提供 16 个点的观看余量?

ios - 在 Objective-C 中,如何以编程方式获取设备的 BuildID

iphone - iOS:更改设备音量