tosi
tosi

Reputation: 1925

TableView and Custom Cells

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.

Upvotes: 0

Views: 255

Answers (2)

JNK
JNK

Reputation: 1753

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

Upvotes: 1

Farlei Heinen
Farlei Heinen

Reputation: 5899

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

Upvotes: 0

Related Questions