iOS UITableView的横向滑动
在开发中横向滑动我们通常会想到用UICollectionView,确实这个好用,但有时候需求不太明确而且用UICollectionView的头部需要自定义没有UITableView简单,粽子看需求,根据需求决定,但是我们得会这种技能。
@interfaceViewController()<UITableViewDelegate,UITableViewDataSource>@property(strong,nonatomic)UITableView*myTableView;@end@implementationViewController-(UITableView*)myTableView{if(!_myTableView){CGRecttableViewRect=CGRectMake(0,0,100,CGRectGetWidth(self.view.frame));_myTableView=[[UITableViewalloc]initWithFrame:CGRectZerostyle:UITableViewStylePlain];_myTableView.dataSource=self;_myTableView.delegate=self;_myTableView.frame=tableViewRect;_myTableView.separatorStyle=NO;_myTableView.backgroundColor=[UIColorgrayColor];_myTableView.transform=CGAffineTransformMakeRotation(-M_PI/2);_myTableView.showsVerticalScrollIndicator=NO;_myTableView.center=CGPointMake(self.view.frame.size.width/2,50);}return_myTableView;}-(void)viewDidLoad{[superviewDidLoad];//AppDelegate进行全局设置if(@available(iOS11.0,*)){[[UIScrollViewappearance]setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];}self.view.backgroundColor=[UIColorpurpleColor];[self.viewaddSubview:self.myTableView];//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.}-(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath{return100;}-(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{return50;}-(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{FFTableViewCell*cell=[FFTableViewCellcellWithTableView:tableView];returncell;}#pragmamark选中的方法-(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{[tableViewdeselectRowAtIndexPath:indexPathanimated:YES];}#import"FFTableViewCell.h"@interfaceFFTableViewCell()@property(strong,nonatomic)UIButton*monthBtn;@end@implementationFFTableViewCellstaticNSString*cellID=@"FFTableViewCell";-(UIButton*)monthBtn{if(!_monthBtn){_monthBtn=[UIButtonbuttonWithType:UIButtonTypeCustom];_monthBtn.backgroundColor=[UIColorredColor];_monthBtn.layer.cornerRadius=30.0f;_monthBtn.clipsToBounds=YES;[_monthBtnsetTitle:@"在干嘛"forState:UIControlStateNormal];}return_monthBtn;}-(void)awakeFromNib{[superawakeFromNib];//Initializationcode}+(instancetype)cellWithTableView:(UITableView*)tableView{FFTableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:cellID];if(cell==nil){cell=[[FFTableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellID];}returncell;}-(instancetype)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString*)reuseIdentifier{self=[superinitWithStyle:stylereuseIdentifier:reuseIdentifier];if(self){self.contentView.transform=CGAffineTransformMakeRotation(M_PI/2);[self.contentViewaddSubview:self.monthBtn];}returnself;}-(void)layoutSubviews{[superlayoutSubviews];_monthBtn.frame=CGRectMake((CGRectGetWidth(self.contentView.frame)-60)/2,(CGRectGetHeight(self.contentView.frame)-60)/2,60,60);}-(void)setSelected:(BOOL)selectedanimated:(BOOL)animated{[supersetSelected:selectedanimated:animated];//Configuretheviewfortheselectedstate}-(void)dealloc{_monthBtn=nil;}@end
解决问题的方法不止一种,要多想想其他的解决办法,这样才能更好的掌握每个知识点,说不定会更好,说不定就是面试题等待。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。