iOS UITableView的橫向滑動

來源:互聯網
上載者:User

標籤:icon   rect   getheight   etc   after   試題   source   setup   gate   

    在開發中橫向滑動我們通常會想到用UICollectionView,確實這個好用,但有時候需求不太明確而且用UICollectionView的頭部需要自訂沒有UITableView簡單,粽子看需求,根據需求決定,但是我們得會這種技能。

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>@property(strong,nonatomic)UITableView *myTableView;@end@implementation ViewController- (UITableView *)myTableView{    if(!_myTableView){        CGRect tableViewRect = CGRectMake(0, 0,100, CGRectGetWidth(self.view.frame));        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];        _myTableView.dataSource = self;        _myTableView.delegate = self;        _myTableView.frame = tableViewRect;        _myTableView.separatorStyle = NO;        _myTableView.backgroundColor = [UIColor grayColor];        _myTableView.transform = CGAffineTransformMakeRotation(-M_PI / 2);        _myTableView.showsVerticalScrollIndicator = NO;        _myTableView.center = CGPointMake(self.view.frame.size.width / 2, 50);    }    return _myTableView;}- (void)viewDidLoad {    [super viewDidLoad];            //AppDelegate 進行全域設定    if (@available(iOS 11.0, *)){        [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];    }        self.view.backgroundColor = [UIColor purpleColor];    [self.view addSubview:self.myTableView];    // Do any additional setup after loading the view, typically from a nib.}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 100;}-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return  50;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{        FFTableViewCell *cell = [FFTableViewCell cellWithTableView:tableView];    return cell;}#pragma mark 選中的方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    [tableView deselectRowAtIndexPath:indexPath animated:YES];}#import "FFTableViewCell.h"@interface FFTableViewCell()@property(strong,nonatomic)UIButton *monthBtn;@end@implementation FFTableViewCellstatic NSString *cellID = @"FFTableViewCell";- (UIButton *)monthBtn{    if(!_monthBtn){        _monthBtn = [UIButton buttonWithType:UIButtonTypeCustom];        _monthBtn.backgroundColor = [UIColor redColor];        _monthBtn.layer.cornerRadius = 30.0f;        _monthBtn.clipsToBounds = YES;        [_monthBtn setTitle:@"在幹嘛" forState:UIControlStateNormal];    }    return _monthBtn;}- (void)awakeFromNib {    [super awakeFromNib];    // Initialization code}+ (instancetype)cellWithTableView:(UITableView *)tableView{    FFTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellID];    if (cell == nil) {        cell  = [[FFTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];    }    return cell;}- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if(self){                self.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2);        [self.contentView addSubview:self.monthBtn];    }    return self;}- (void)layoutSubviews{    [super layoutSubviews];    _monthBtn.frame = CGRectMake((CGRectGetWidth(self.contentView.frame)-60)/2, (CGRectGetHeight(self.contentView.frame)-60)/2, 60,60);}- (void)setSelected:(BOOL)selected animated:(BOOL)animated {    [super setSelected:selected animated:animated];    // Configure the view for the selected state}- (void)dealloc{    _monthBtn = nil;}@end

解決問題的方法不止一種,要多想想其他的解決辦法,這樣才能更好的掌握每個知識點,說不定會更好,說不定就是面試題等待。

iOS UITableView的橫向滑動

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.