ios UITableView和自定義UITableViewCell
阿新 • • 發佈:2019-01-27
1、自定義cell,FindCell.h
#ifndef FindCell_h #define FindCell_h #import <UIKit/UIKit.h> @interface FindCell : UITableViewCell @property (strong,nonatomic) UIImageView *leftImage; @property (strong,nonatomic) UILabel *titleLable; @property (strong,nonatomic) UILabel *dateLabel; @end #endif /* FindCell_h */
FindCell.m
#import "FindCell.h" @interface FindCell () @end @implementation FindCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; if (self) { [self initLayout]; } return self; } - (void)initLayout { self.leftImage = [[UIImageView alloc] initWithFrame:CGRectMake(16, 10, 100, 100)]; self.leftImage.backgroundColor = [UIColor orangeColor]; self.leftImage.layer.masksToBounds = YES; self.leftImage.layer.cornerRadius = 50; // cell提供了一個contentView的屬性,專門用來自定義cell,防止在cell佈局的時候發生佈局混亂,如果是自定義cell,記得將子控制元件新增到ContentView上 [self.contentView addSubview:self.leftImage]; self.titleLable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.leftImage.frame) + 10, CGRectGetMinY(self.leftImage.frame), 100, 40)]; self.titleLable.backgroundColor = [UIColor redColor]; [self.contentView addSubview:self.titleLable]; self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.titleLable.frame), CGRectGetMaxY(self.titleLable.frame) + 20, 200, 40)]; self.dateLabel.backgroundColor = [UIColor greenColor]; [self.contentView addSubview:self.dateLabel]; } @end
2、呼叫
#ifndef FindViewController_h
#define FindViewController_h
#import <UIKit/UIKit.h>
@interface FindViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@end
#endif /* FindViewController_h */
#import "FindViewController.h" #import "FindCell.h" @interface FindViewController () @property (weak, nonatomic) IBOutlet UITableView *tableView; @property (nonatomic, strong) NSMutableArray *allContactsArray; @property (weak, nonatomic) IBOutlet UISearchBar *search; @end @implementation FindViewController - (void)viewDidLoad { [super viewDidLoad]; self.tableView.delegate = self; self.tableView.dataSource = self; self.search.delegate = self; self.tableView.rowHeight = 70; //// 預設行高 YES; //// 將SearchBar繫結到TableView表頭 self.tableView.tableHeaderView = self.search; self.allContactsArray = [[NSMutableArray alloc]init]; [self handleData]; } - (void)handleData { FindCell *findCell = nil; for (int i = 0; i < 20; i ++) { findCell = [[FindCell alloc] init]; [findCell.leftImage setImage:[UIImage imageNamed:@"我的.pdf"]]; findCell.titleLable.text = @"標題"; findCell.locationLabel.text = @"一區"; findCell.dateLabel.text = @"20180601"; [self.allContactsArray addObject:findCell]; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } //// 數量少向上滑動控制在首行 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y <= 0.01) { scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0); } else if (scrollView.contentOffset.y >= 0.01){ scrollView.contentInset = UIEdgeInsetsMake(-0.01, 0, 0, 0); } } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.allContactsArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } // 在有cell資料時候生效 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 70; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *identifier = @"cell"; FindCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { cell = [[FindCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]; } FindCell *findCell = self.allContactsArray[indexPath.section]; //// cell點選時顏色 findCell.selectionStyle = UITableViewCellSelectionStyleGray; return findCell; } // cell點選時 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //// 離開時消除顏色 [tableView deselectRowAtIndexPath:indexPath animated:YES]; } //// 側滑刪除 - ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath { UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"刪除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) { //[tableView setEditing:YES animated:YES]; [self.allContactsArray removeObjectAtIndex:indexPath.section]; completionHandler (YES); [self.tableView reloadData]; }]; // deleteRowAction.image = [UIImage imageNamed:@"刪除"]; // deleteRowAction.backgroundColor = [UIColor redColor]; UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]]; config.performsFirstActionWithFullSwipe = NO; //// 禁止超長左滑直接刪除 return config; } //// 點選搜尋後收起鍵盤 -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [searchBar resignFirstResponder]; //searchBar失去焦點 } @end
關於側滑刪除,會出現崩潰的顯現,下面的寫法,沒有動畫效果,但是不會出現崩潰的情況:
//// 側滑刪除
- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"刪除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
[self.allContactsArray removeObjectAtIndex:indexPath.section];
[self.tableView reloadData];
}];
UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
config.performsFirstActionWithFullSwipe = NO; //// 禁止超長左滑直接刪除
return config;
}