TableView和自定义单元格

时间:2010-11-17 12:02:55

标签: iphone tableview cells

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

2 个答案:

答案 0 :(得分:1)

我就是这样做的......

tableView.h:

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

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

@end

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

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]];

变化:

- (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]];
}

答案 1 :(得分:0)

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

选择单元格后,切换动画块内的视图