Changing cell contents in UITableView while listening

I am creating a table view application. But I want it to be a table view that expands the cell when you click on it and closes it when you click the second time.

But I was wondering if the following is possible. When a cell is not selected, you only see the image, the title and the beginning of the text. But as soon as the cell is selected, it will expand and display even more subspecies, i.e. Images.

Is it possible? For example, to hide a subitem in a cell, and as soon as it is tapped, will it be visible and correctly aligned? And of course, how to do it?

Thnx !!!

+4
source share
2 answers

I did something similar quite a while ago. You will find the github code.

Please note that this is very rude, because where my starting days are iPhone, i.e. properties are missing.

.h

#import <UIKit/UIKit.h> @interface FirstViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> { NSIndexPath *selectedIndexPath; NSDictionary *articles; } @end 

.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. // eg 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 
+6
source

Yes, I do this in the application I'm working on now.

You need to track the state the cell is in, open or closed. If only one cell can be opened at a time, you can do this by simply providing a link to the current index. If several cells can be opened at the same time, you will need an array of logic elements that keeps track of whether each of them is open or closed.

In heightForRowAtIndexPath, simply return the correct height based on whether the row is open or closed.

In cellForRowAtIndexPath, if the row is closed, hide all content that should not be visible when it is closed. Views can still be there, but they must be set to hidden = YES .

Finally, in the didSelectRowAtIndexPath file, specify the specified pointer path if it was closed and closed if it was open, then reload the cell using [tableView reloadRowsAtIndexPaths:] . If you allow only 1 time to open, then just set the current open path of the pointer to the one that was selected, and reload both the one that was selected and the one that was previously opened.

+4
source

Source: https://habr.com/ru/post/1347588/


All Articles