TableView 和自定义单元格

发布于 2024-10-02 17:51:19 字数 180 浏览 2 评论 0原文

我需要有人帮助我在使用 didSelectRowAtIndexPath 选择时切换自定义单元格,因此想法是将自定义 cell1 设置为状态,并且当用户选择它,这将消失,cell2 将淡入其中,显示该州的首都,但除所选单元格之外的其他单元格将保留为 cell1(或州)。

I am in a need for someone to help me out on switching custom cells when selected using didSelectRowAtIndexPath, so the idea is to have custom cell1 set as a state and when the user selects it this will disappear and cell2 will fade in which shows the capital of that state but other cells will remain as cell1 (or states) except the one which was selected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小耗子 2024-10-09 17:51:19

这就是我要做的...

tableView.h:

#import <UIKit/UIKit.h>
@interface tableView : UITableViewController {
    NSMutableArray *tableArray;
}

- (void)changeTitleAtIndexPath:(NSIndexPath *)indexPath;

@end

tableview.m (添加这些方法):

在 -(void)viewDidLoad 添加:

    tableArray = [[NSMutableArray alloc] init];
    [tableArray addObject:[NSMutableArray arrayWithObjects:[NSNumber numberWithBool:NO],@"City1",@"Capital1",nil]];
    [tableArray addObject:[NSMutableArray arrayWithObjects:[NSNumber numberWithBool:NO],@"City2",@"Capital2",nil]];
    [tableArray addObject:[NSMutableArray arrayWithObjects:[NSNumber numberWithBool:NO],@"City3",@"Capital3",nil]];
    [tableArray addObject:[NSMutableArray arrayWithObjects:[NSNumber numberWithBool:NO],@"City4",@"Capital4",nil]];

更改:

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

    static NSString *CellIdentifier = @"Cell";

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

    if (![[[tableArray objectAtIndex:indexPath.row] objectAtIndex:0] boolValue]) {
        cell.textLabel.text = [[tableArray objectAtIndex:indexPath.row] objectAtIndex:1];
    }else {
        cell.textLabel.text = [[tableArray objectAtIndex:indexPath.row] objectAtIndex:2]; 
    }




    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self changeTitleAtIndexPath:indexPath];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

添加:

- (void)changeTitleAtIndexPath:(NSIndexPath *)indexPath{
    BOOL newBool = ![[[tableArray objectAtIndex:indexPath.row] objectAtIndex:0] boolValue];
    [[tableArray objectAtIndex:indexPath.row] replaceObjectAtIndex:0 withObject:[NSNumber numberWithBool:newBool]];
}

This is how I'd do it...

tableView.h:

#import <UIKit/UIKit.h>
@interface tableView : UITableViewController {
    NSMutableArray *tableArray;
}

- (void)changeTitleAtIndexPath:(NSIndexPath *)indexPath;

@end

tableview.m (add these methods) :

in -(void)viewDidLoad add:

    tableArray = [[NSMutableArray alloc] init];
    [tableArray addObject:[NSMutableArray arrayWithObjects:[NSNumber numberWithBool:NO],@"City1",@"Capital1",nil]];
    [tableArray addObject:[NSMutableArray arrayWithObjects:[NSNumber numberWithBool:NO],@"City2",@"Capital2",nil]];
    [tableArray addObject:[NSMutableArray arrayWithObjects:[NSNumber numberWithBool:NO],@"City3",@"Capital3",nil]];
    [tableArray addObject:[NSMutableArray arrayWithObjects:[NSNumber numberWithBool:NO],@"City4",@"Capital4",nil]];

change:

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

    static NSString *CellIdentifier = @"Cell";

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

    if (![[[tableArray objectAtIndex:indexPath.row] objectAtIndex:0] boolValue]) {
        cell.textLabel.text = [[tableArray objectAtIndex:indexPath.row] objectAtIndex:1];
    }else {
        cell.textLabel.text = [[tableArray objectAtIndex:indexPath.row] objectAtIndex:2]; 
    }




    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self changeTitleAtIndexPath:indexPath];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

add:

- (void)changeTitleAtIndexPath:(NSIndexPath *)indexPath{
    BOOL newBool = ![[[tableArray objectAtIndex:indexPath.row] objectAtIndex:0] boolValue];
    [[tableArray objectAtIndex:indexPath.row] replaceObjectAtIndex:0 withObject:[NSNumber numberWithBool:newBool]];
}
独自←快乐 2024-10-09 17:51:19

您必须仅使用一个单元格,并具有多个视图(某些视图的 alpha 设置为透明或隐藏),

当选择该单元格时,在动画块内切换视图

you must use only one cell, with multiples views (some views with alpha set to transparent, or hide)

when the cell is selected, switch the views inside an animation block

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文