UITableView click on each Cell, the Cell sub-content, and uitableviewcell

Source: Internet
Author: User

UITableView click on each Cell, the Cell sub-content, and uitableviewcell

The first thought of getting TableviewCell's sub-content is,

Method 1:

Use the UITableview proxy to process the corresponding expansion and collapse:

1. Proxy:-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath

2. You need to declare a global BOOL variable isOpen, record the current cell status (expand/collapse), declare an NSInterge type selectedIndexRow, record the row of the selected cell, and remember to initialize the corresponding attributes.

(Ps: I have seen many articles on the Internet, but when I use them, I find that we need to declare another NSIndexPath selectedIndex, or you can use the row generated record by yourself at that time, maybe I did it myself)

3. first, we need to clarify the logical relationship of our own needs, when to expand/collapse, when to expand and collapse its height, number, and so on what changes -------> to conduct proxy, data source method writing

The following is the call sequence for tableview.

1>: // number of returned Cells
-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section
2>: // returns the height of each row.
-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath
3>: // request the data element proxy to insert the desired cell for tableView
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
4>: // monitor the clicked cell
-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath

Code:

# Pragma mark --------- UITableViewDelegate & UITableViewDataSource -----

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

{

Return self. scoreDataArray. count; // return the result based on your specific needs.

}

// Computing height --- dynamically calculate the height of the content as needed

-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath

{

// IndexPath. row = selectedIndex. row &&

NSLog (@ "heightForRow % ld", (long) selectedIndex. row );

If (indexPath. row = selectedIndex. row & selectedIndex! = Nil)

{

If (isOpen = YES)

{

// Ignore the internal content.

YunGangScoreData * data = [[YunGangScoreData alloc] init];

// Data = self. scoreDataArray [indexPath. row];

Data = self. scoreDataArray [selectedIndex. row];

If (data. detailTypeArray. count> 0)

{

Self. cellHeight = 60 + data. detailTypeArray. count * 40;

Return self. cellHeight;

}

Return 60;

}

Else

{

Return 60;

}

}

Return 60;

}

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

{

// IndexPath. row = selectedIndex. row &&

NSLog (@ "cellForRow % ld", (long) selectedIndex. row );

If (indexPath. row = selectedIndex. row & selectedIndex! = Nil)

{// Ignore internal content

// Expand

If (isOpen = YES)

{

YunGangScoreTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: @ "YunGangScoreTableViewCell"];

Cell. selectionStyle = UITableViewCellSelectionStyleNone;

Cell. data = self. scoreDataArray [selectedIndex. row];

Cell. isOpen = YES;

Return cell;

}

Else

{// Hide

YunGangScoreTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: @ "YunGangScoreTableViewCell"];

Cell. selectionStyle = UITableViewCellSelectionStyleNone;

Cell. data = self. scoreDataArray [selectedIndex. row];

Cell. isOpen = NO;

Return cell;

}

}

Else // not yourself

{

YunGangScoreTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: @ "YunGangScoreTableViewCell"];

Cell. selectionStyle = UITableViewCellSelectionStyleNone;

Cell. data = self. scoreDataArray [indexPath. row];

Cell. isOpen = NO;

Return cell;

}

}

-(Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath

{

// Add the index to the array

NSArray * indexPaths = [NSArray arrayWithObject: indexPath];

// When you select different row states

// I added it myself and read a lot of articles. Maybe I am wrong. selectedIndex = indexPath;

NSLog (@ "% ld", (long) selectedIndex. row );

If (selectedIndex! = Nil & indexPath. row = selectedIndex. row)

{// Add the selected and all indexes to the array

// IndexPaths = [NSArray arrayWithObjects: indexPath, selectedIndex, nil];

IsOpen =! IsOpen;

}

Else if (selectedIndex! = Nil & indexPath. row! = SelectedIndex. row)

{

IndexPaths = [NSArray arrayWithObjects: indexPath, selectedIndex, nil];

IsOpen = YES;

}

// Write down the selected index

SelectedIndex = indexPath;

// Refresh

[TableView reloadRowsAtIndexPaths: indexPaths withRowAnimation: UITableViewRowAnimationFade];

}

The general use of this content, look at the debugging...

Method 2: I tried the above method and found that there may be issues such as refreshing, so I changed the following

Here, use section as the first cell, load the view to the section, and add a gesture to load the cell as a click event. (the second one)

Directly run the Code:


1. Declare attributes:

// Cell storage and recording of cell selectedIndex

NSMutableDictionary * _ showDic; // used to determine whether a group is expanded or shrunk.

Int index;

2. Use the number of sections to display the tableview content when no click is made.

3. Calculate the number of cells after each section.

4. Calculate the height of the split cell based on your needs and the returned content.

5. Load the cell content after the click, and transmit the data information of the clicked section.

6. Calculate the height required by each section as needed (the last section here is required)

7. Load the content of each section. Note that the loaded view inherits

UITableViewHeaderFooterView

-(UIView *) tableView :( UITableView *) tableView viewForHeaderInSection :( NSInteger) section

{

NSArray * objs = [[NSBundle mainBundle] loadNibNamed: @ "YunGangScoreTableViewCell" owner: nil options: nil];

YunGangScoreTableViewCell * cell = [objs objectAtIndex: 0];

If (self. scoreDataArray. count> 0)

{// Ensure data exists

YunGangScoreData * data = self. scoreDataArray [section];

Cell. data = data;

}

// Click Recognizer to contract the Group cell

Cell. tag = section;

UITapGestureRecognizer * singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (SingleTap1 :)];

SingleRecognizer. numberOfTapsRequired = 1; // Number of clicks = 1: Click

[SingleRecognizer setNumberOfTouchesRequired: 1]; // one finger operation

[Cell addGestureRecognizer: singleRecognizer]; // Add a gesture monitor;

Return cell;

}

In addition, to adapt to the problem, we need to change the frame of the view in the Custom view.

8. Click gesture events and handling

 

9. Note that _ showDic needs to be cleared every time you request data to refresh tableview.

You can add and modify other content as needed. The two methods are tested, and the first one is normal. After refresh, it seems that there is confusion. If you need to use them, modify the content as needed, the second method can be used for testing. If there is no major problem, to meet the requirements, we will post the following:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.