G.P.Reddy February 2016

Issue on TableView Cell

Here i'm developing a expanded TableViewCell ,while TableView didSelectRowAtIndexPath method i am using to cell expand, here i used two TableView Cell's enter image description here

but i need to the button action SecondCell will be expanded.Can you please help how can i implement here is the below code,

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        // disable touch on expanded cell
    UITableViewCell *cell = [self.theTableView cellForRowAtIndexPath:indexPath];
    if ([[cell reuseIdentifier] isEqualToString:@"ExpandedCellIdentifier"]) {
        return;
    }

        // deselect row
    [tableView deselectRowAtIndexPath:indexPath
                             animated:NO];

        // get the actual index path
    indexPath = [self actualIndexPathForTappedIndexPath:indexPath];

        // save the expanded cell to delete it later
    NSIndexPath *theExpandedIndexPath = self.expandedIndexPath;

        // same row tapped twice - get rid of the expanded cell
    if ([indexPath isEqual:self.expandingIndexPath]) {
        self.expandingIndexPath = nil;
        self.expandedIndexPath = nil;
    }
        // add the expanded cell
    else {
        self.expandingIndexPath = indexPath;
        self.expandedIndexPath = [NSIndexPath indexPathForRow:[indexPath row] + 1
                                                    inSection:[indexPath section]];
    }

    [tableView beginUpdates];

    if (theExpandedIndexPath) {
        [_theTableView deleteRowsAtIndexPaths:@[theExpandedIndexPath]
                             withRowAnimation:UITableViewRowAnimationNone];
    }
    if (self.expandedIndexPath) {
        [_theTableView insertRowsAtIndexPaths:@[self.expandedIndexPath]
                             withRowAnimation:UITableViewRowAnimationNone];
           

Answers


madhuiOS February 2016

- (IBAction)expand:(id)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.theTableView];

    NSIndexPath *indexPathInner = [self.theTableView indexPathForRowAtPoint:buttonPosition];
  // disable touch on expanded cell
    UITableViewCell *cell = [self.theTableView cellForRowAtIndexPath:indexPathInner];
    if ([[cell reuseIdentifier] isEqualToString:@"ExpandedCellIdentifier"]) {
        return;
    }

        // deselect row
    [tableView deselectRowAtIndexPath:indexPath
                             animated:NO];

        // get the actual index path
    indexPath = [self actualIndexPathForTappedIndexPath:indexPath];

        // save the expanded cell to delete it later
    NSIndexPath *theExpandedIndexPath = self.expandedIndexPath;

        // same row tapped twice - get rid of the expanded cell
    if ([indexPath isEqual:self.expandingIndexPath]) {
        self.expandingIndexPath = nil;
        self.expandedIndexPath = nil;
    }
        // add the expanded cell
    else {
        self.expandingIndexPath = indexPath;
        self.expandedIndexPath = [NSIndexPath indexPathForRow:[indexPath row] + 1
                                                    inSection:[indexPath section]];
    }

    [tableView beginUpdates];

    if (theExpandedIndexPath) {
        [_theTableView deleteRowsAtIndexPaths:@[theExpandedIndexPath]
                             withRowAnimation:UITableViewRowAnimationNone];
    }
    if (self.expandedIndexPath) {
        [_theTableView insertRowsAtIndexPaths:@[self.expandedIndexPath]
                             withRowAnimation:UITableViewRowAnimationNone];
    }

    [tableView endUpdates];

        // scroll to the expanded cell
    [self.theTableView scrollToRowAtIndexPath:indexPath
                             atScrollPosition:UITableViewScrollPositionMiddle
                                     animated:YES];

}


Aju February 2016

I go through you code , and i think you need only to get the cell. see the below code for getting it.

CGPoint touchPoint = [textField convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

Now do the same steps what you did in didSelectRowAtIndexPath.


Pradheep Narendran P February 2016

This is my solution which you can achieve the logic easily by creating a separate .xib file by disabling size classes. Use a boolean variable to check whether it is selected. If so, then reload the specific cell and use a delegate method heightforrow and determine that boolean variable here. It is simple... Hope you understood. If not, please do let me know. I'll share my code which I worked out.

Edited the post and below is my code :-

    @implementation ViewController
    BOOL sel=NO;
    NSMutableArray *a,*b;
    NSIndexPath *path;
    - (void)viewDidLoad {
        [super viewDidLoad];
        a=[[NSMutableArray alloc] initWithObjects:@"Apple",@"Mango",@"Orange",@"Pineapple",@"Cricket",@"Bike",@"Music", nil];
        b=[[NSMutableArray alloc] initWithArray:a copyItems:YES];
            [self.tview registerNib:[UINib nibWithNibName:@"cell1" bundle:nil] forCellReuseIdentifier:@"cell"];
        // Do any additional setup after loading the view, typically from a nib.
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

        return a.count;
    }


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


    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        [tableView beginUpdates];
        if([[a objectAtIndex:indexPath.row] isEqualToString:@"Selected"]){
            sel=NO;
        [a replaceObjectAtIndex:indexPath.row withObject:[b objectAtIndex:indexPath.row]];
        }
        else{
        [a replaceObjectAtIndex:indexPath.row withObject:@"Selected"];
            sel=YES;
        }

        [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        [tableView endUpdates];
    }

    -(CGFloat)tableView:(UITableVi 


iOSEnthusiatic February 2016

If you want to expand/collapse your tableView cell then, I have made a demo that gives you the exact behaviour as you want. here is the link: https://github.com/rushisangani/TableViewCellExpand

Please set constraint of your expand/collapse views as described in demo.

Its based on the Content Hugging and Content Compression Resistance Priority.

Post Status

Asked in February 2016
Viewed 3,453 times
Voted 14
Answered 4 times

Search




Leave an answer