Folding and opening the cell
www.jianshu.com/p/202b5cfcc…
Custom Cell Background color when selected
cell.selectedBackgroundView = [UIView new];
cell.selectedBackgroundView.backgroundColor = [UIColor xxxxxx];
Copy the code
Refreshes a cell or section
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
Copy the code
Check whether a row of cells is displayed
CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
BOOL completelyVisible = CGRectContainsRect(tableView.bounds, cellRect);
Copy the code
Judge that the cell is on the screen
1.-(NSArray*)visibleCells; The UITableView method, which is the most straightforward, returns an array of UITableView Cells. For custom cells, later processing can be a little more tedious.
2.- (NSArray*)indexPathsForVisibleRows; Another method of UITableview, this one’s a little easier to use, returns an array of NSIndexPath, so you can call the data in your table_related_Array directly with indexpath. Row. More convenient, used for custom cell.
3. This method can be used in designs with a lot of proxy callbacks
- (CGRect)rectForRowAtIndexPath:(NSIndexPath*)indexPath; CGRect cellR = [myTV rectForRowAtIndexPath:indx]; if (myTV.contentOffset.y - cellR.origin.y < myCell.frame.size.height || cellR.origin.y - myTV.contentOffset.y > mytv.size.height) {// myCell is no longer visible in myTV. } else {//myCell is visible, business processing}Copy the code
Load network image optimization
Thought: stop rolling when loaded from www.jianshu.com/p/328e50390… I don’t think the needs are suitable
A cell
UITableViewCell *cell = [weakSelf.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]]; Or - (void)scrollViewDidScroll (UIScrollView *)scrollView{if (scrollView == _rightTableView && _isSelected == NO) { // The system method returns indexPath NSIndexPath * indexPath = [_rightTableView] for the cell at a coordinate of the tableView indexPathForRowAtPoint:scrollView.contentOffset]; NSLog(@" slide to %ld group % LD ", indexpa.section, indexpa.row); _currentIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section]; [_leftTableView reloadData]; [_leftTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.section] atScrollPosition:UITableViewScrollPositionMiddle animated:NO]; }}Copy the code
Customize the right icon of a cell
Self. AccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@" checkmark "]]; // Benefit: no need to layout againCopy the code
Click the child control of the cell to obtain the corresponding cell
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
UITableViewCell *cell = (UITableViewCell*)[self.tableView cellForRowAtIndexPath:indexPath];
Copy the code
Select scroll to a certain line of cell
[self.myTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
Copy the code
By default, a row of cells is selected
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { PDNetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PDNetCell" forIndexPath:indexPath]; cell.model = [self.gridArr objectAtIndex:indexPath.row]; // Default selected line (key code) if (! isInit) { NSIndexPath *firstPath = [NSIndexPath indexPathForRow:0 inSection:0]; [tableView selectRowAtIndexPath:firstPath animated:YES scrollPosition:UITableViewScrollPositionNone]; if ([tableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) { [tableView.delegate tableView:tableView didSelectRowAtIndexPath:firstPath]; } isInit = YES; } return cell;} return cell; } // In the cell. M file method, Show the selected style - (void)setSelected:(BOOL)selected animated:(BOOL)animated {[super setSelected:selected animated:animated]; / /... }Copy the code
The style when the cell is selected
/ / colorless cell. SelectionStyle = UITableViewCellSelectionStyleNone; / / blue cell. SelectionStyle = UITableViewCellSelectionStyleBlue; / / gray cell. SelectionStyle = UITableViewCellSelectionStyleGray;Copy the code
Set the spacing between cells
- (void)setFrame:(CGRect)frame {frame.size.height -= 20; [super setFrame:frame]; }Copy the code
Insert data
NSMutableArray *insertion = [NSMutableArray arrayWithCapacity:0];
for (int i = 0; i < tmpGoodsList.goods.count; i++) {
[insertion addObject:[NSIndexPath indexPathForRow:tmpcount + i inSection:3]];
}
[self.rushTableView insertRowsAtIndexPaths:insertion withRowAnimation:UITableViewRowAnimationMiddle];
Copy the code
The screen is not full of data, so the extra Cell is hidden
self.tableView.tableFooterView = [[UIView alloc]init];
Copy the code
The dividing line is set to the top (default starts with 15 pixels empty)
www.titanjun.top/2016/11/20/…
cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
Copy the code
Scroll to a line
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:NSNotFound inSection:2] atScrollPosition:UITableViewScrollPositionTop animated:YES];
Copy the code
Click the cell to automatically scroll to the next line
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{// the first method [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; / / the second method [self tableVieW selectRowAtIndexPath: indexPath animated: YES scrollPosition: UITableViewScrollPositionTop]; }Copy the code
Update the line height
- Cell to add an attribute
@property(nonatomic,assign)float cellH;
2. Set a block callback to refresh the row height @property(nonatomic,strong)void(^heightReback_Block)(float cellH);
3. Call block to start the refresh
cell.heightReback_Block = ^(float cellH) { _cellH = cellH; / / update the line height [tableView reloadRowsAtIndexPaths: [NSArray arrayWithObjects: indexPath, nil] withRowAnimation:UITableViewRowAnimationBottom]; }; -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{return _cellH; }Copy the code
Cell highly adaptive 1 *
/ / implement proxy method can - (CGFloat) tableView: (UITableView *) tableView estimatedHeightForRowAtIndexPath: (indexPath NSIndexPath *) { return 100; } // Note: The layout of the cell child control!Copy the code
Cell highly adaptive 2
When using frame layouts, this usually adds a secondary property, cellHeight, to your model. Override the get method on this property in the model to calculate the total height based on the values of your layout and other properties in the model. Finally, in the tableView: heightForRow method, find the corresponding model according to the indexPath, and return this height.
Cell highly adaptive 3 *
/ / forecast line high self. TableView. RowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 150; . - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0 || indexPath.section == 1) { return 81; } / / solve the fixed line height and the system automatically calculate the other groups to go high line system automatically calculate line high return UITableViewAutomaticDimension; }Copy the code
Cache cell height by yourself
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { BSQuestionsModel * model = _dataArray[indexPath.section]; return model.cell_height? :UITableViewAutomaticDimension; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { BSQuestionsModel * model = _dataArray[indexPath.section]; BSQuestionsTableViewCell * cell = [BSQuestionsTableViewCell cellForTableView:tableView model:model]; / / height cache CGFloat height = [cell systemLayoutSizeFittingSize: CGSizeMake (tableView. Frame. The size. The width, 0) withHorizontalFittingPriority:UILayoutPriorityRequired verticalFittingPriority:UILayoutPriorityFittingSizeLevel].height; model.cell_height = height; return cell; }Copy the code
You can select multiple options in edit mode
self.myTableView.allowsMultipleSelectionDuringEditing = YES;
Copy the code
- (IBAction)edit:(id)sender { [self.myTableView setEditing:!self.myTableView.isEditing animated:YES]; Self.deletebtn. hidden =! Self.deletebtn. hidden =! self.myTableView.isEditing; }Copy the code
- (IBAction)delete:(id)sender { NSMutableArray *deleArr = [NSMutableArray array]; for(NSIndexPath *indx in self.myTableView.indexPathsForSelectedRows){ [deleArr addObject:self.girlArray[indx.row]]; / / get the selected row} [self girlArray removeObjectsInArray: deleArr]; // delete them from the model // [self.myTableView reloadData]; / / / / refresh data animation refresh data [self. MyTableView deleteRowsAtIndexPaths: self. MyTableView. IndexPathsForSelectedRows withRowAnimation:UITableViewRowAnimationAutomatic]; }Copy the code
Scroll to a certain line of cells
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:NSNotFound inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
Copy the code
Drop down to enlarge the header image
Third party: github.com/iThinkerYZ/… 1. Create a tableview
. / / note that setting surrounded by spacing (bottom right) on the self. The tableView. ContentInset = UIEdgeInsetsMake (0.2 * screenH, 0, 0, 0).Copy the code
2. Create the image img View
UIImageView * iimgV = [[UIImageView alloc] initWithFrame: CGRectMake(0, -0.2*screenH, screenW, 0.2*screenH)]; Iimgv. image = [UIImage imageNamed:@"myHeader"]; iimgv.contentMode = UIViewContentModeScaleAspectFill; // key [self.tableView addSubview:iimgv]; // Add self.iimgv = iimgv;Copy the code
3. Zoom down
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ CGFloat y = self.tableView.contentOffset.y; If (y < -0.234*screenH) {CGRect frame = self.iimgv.frame; frame.origin.y = y; frame.size.height = - y; self.iimgv.frame = frame; } return; }Copy the code
4. The same applies to ScrollView.
Abstract base class
Design with the model, with the logic of a variety of cell www.jianshu.com/p/f308c43fb…