温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS UITableView的横向滑动

发布时间:2020-07-02 03:57:20 来源:网络 阅读:3018 作者:大头狼小鬼 栏目:移动开发

    在开发中横向滑动我们通常会想到用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 FFTableViewCell

static 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

解决问题的方法不止一种,要多想想其他的解决办法,这样才能更好的掌握每个知识点,说不定会更好,说不定就是面试题等待。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI