简体   繁体   English

点击时更改UITableView中的单元格内容

[英]Changing cell content in UITableView when tapped

I'm building a app which has a table view. 我正在构建一个具有表格视图的应用程序。 But I want this to be a table view which expands the cell when you tap on it and close when you tap a second time. 但我希望这是一个桌面视图,当你点击它时会扩展单元格,当你再次点击时它会关闭。

But I was wondering if the following is possible. 但我想知道以下是否可行。 When the cell isn't selected you only see a picture, title and the beginning of the text. 未选择单元格时,您只能看到图片,标题和文本的开头。 But as soon as the cell is selected, it will expand and show even more subviews, ie image views. 但是一旦选择了单元格,它就会扩展并显示更多的子视图,即图像视图。

Is this possible? 这可能吗? For instance, to hide a subview in the cell, and as soon a it is tapped it's visible and aligned the right way? 例如,要隐藏单元格中的子视图,并且一旦它被点击它就可以看到并以正确的方式对齐? And of course, how do i do that? 当然,我该怎么做?

Thnx!!! 日Thnx !!!

I did something similar quite a few time ago. 不久前我做了类似的事情。 You'll find the code at github . 你会在github找到代码。

Note that it is very rough, as it where my beginning iPhone days, ie properties are missing. 请注意,它非常粗糙,因为它是我开始的iPhone时代,即属性缺失。

.h 。H

#import <UIKit/UIKit.h>


@interface FirstViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
    NSIndexPath *selectedIndexPath;
    NSDictionary *articles;
}

@end

.m .M

#import "FirstViewController.h"
@implementation FirstViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    selectedIndexPath = nil;
    articles = [[NSDictionary dictionaryWithObject:[NSArray arrayWithObjects:@"one", @"two", @"three",
                                                    @"four", @"five", @"six",
                                                    @"seven", @"eight", @"nine",
                                                    @"ten", @"eleven", nil]
                 forKey:@"title"] retain];
}
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
- (void)dealloc {
    [selectedIndexPath release];
    [articles release];
    [super dealloc];
}

- (int)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[articles allKeys] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [[articles allKeys] objectAtIndex : section];
}

- (int)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    id key = [[articles allKeys] objectAtIndex:section];
    return [[articles objectForKey : key] count];
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ((selectedIndexPath != nil) && (selectedIndexPath.row == indexPath.row))
        return 80.0;
    return 40.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * MyIdentifier = @"MyIdentifier";
    UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
    }
    id key = [[articles allKeys] objectAtIndex:indexPath.section];
    cell.textLabel.text = [[articles objectForKey:key] objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (selectedIndexPath == indexPath) {
        selectedIndexPath = nil;
    } else {
        selectedIndexPath = indexPath;
    }
    [self.tableView deselectRowAtIndexPath : indexPath animated : NO];
    [tableView beginUpdates];
    [tableView endUpdates];
}

@end

Yes, I do this in the app I'm working on now. 是的,我在我正在处理的应用程序中执行此操作。

You need to keep track of the state that a cell is in, either open or closed. 您需要跟踪单元格所处的状态,无论是打开还是关闭。 If only 1 cell can be open at a time, you can do this by just keeping a reference to the current indexPath. 如果一次只能打开一个单元格,则只需保留对当前indexPath的引用即可。 If multiple cells can be open at the same time, you'll need an array of booleans, which tracks if each one is open or closed. 如果可以同时打开多个单元格,则需要一组布尔值,用于跟踪每个单元格是打开还是关闭。

In heightForRowAtIndexPath, just return the correct height based on if the row is open or closed. 在heightForRowAtIndexPath中,只需根据行是打开还是关闭返回正确的高度。

In cellForRowAtIndexPath, if the row is closed, hide all the content that shouldn't be visible when it's closed. 在cellForRowAtIndexPath中,如果该行已关闭,则隐藏关闭时不应显示的所有内容。 The views can still be there, but they should be set to hidden = YES . 视图仍然可以存在,但应将它们设置为hidden = YES

Finally, in didSelectRowAtIndexPath, set the given index path to open if it was closed, and closed if it was open, then reload the cell with [tableView reloadRowsAtIndexPaths:] . 最后,在didSelectRowAtIndexPath中,将给定索引路径设置为打开(如果已关闭),如果已打开则关闭,然后使用[tableView reloadRowsAtIndexPaths:]重新加载单元格。 If you are only allowing 1 at a time to be open, then just set your current open index path to the one that was selected, and reload both the one that was selected as well as the one that had been previously open. 如果您一次只允许打开一个,那么只需将当前打开的索引路径设置为所选的路径,然后重新加载选中的路径以及之前打开的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM