iOS UITableView的横向滑动
admin
2023-02-09 13:00:05
0

    在开发中横向滑动我们通常会想到用UICollectionView,确实这个好用,但有时候需求不太明确而且用UICollectionView的头部需要自定义没有UITableView简单,粽子看需求,根据需求决定,但是我们得会这种技能。

@interface ViewController ()

@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

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

相关内容

热门资讯

今日重大发现“皮皮衡阳字牌.到... 网上科普关于“皮皮衡阳字牌有没有挂”话题很是火热,小编也是针对皮皮衡阳字牌作*弊开挂的方法以及开挂对...
【第一财经】“飞鹰互娱.到底是... 有 亲,根据资深记者爆料飞鹰互娱是可以开挂的,确实有挂(咨询软件无需打开...
玩家分享攻略“天天福建麻将.究... 您好:天天福建麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款...
今日重磅消息“白金岛.有没有挂... 网上科普关于“白金岛有没有挂”话题很是火热,小编也是针对白金岛作*弊开挂的方法以及开挂对应的知识点,...
网友反映“冷水江锡矿山青丰河水... 关于网友反映“锡矿山青丰河水体呈黄色”的情况说明2025年12月20日13时许,网友视频反映“娄底小...
终于了解“新人海炸金花.可以开... 网上科普关于“新人海炸金花有没有挂”话题很是火热,小编也是针对新人海炸金花作*弊开挂的方法以及开挂对...
终于了解“新大圣牛牛.可以开挂... 有 亲,根据资深记者爆料新大圣牛牛是可以开挂的,确实有挂(咨询软件无需打...
今日重大发现“约约麻将.有挂吗... 今日重大发现“约约麻将.有挂吗?”果然有透视挂您好,约约麻将这个游戏其实有挂的,确实是有挂的,需要了...
玩家分享攻略“广客账本.辅助器... 家人们!今天小编来为大家解答广客账本透视挂怎么安装这个问题咨询软件客服徽9784099的挂在哪里买很...
联通9元套餐全解析:资费、避坑... 办卡:微 信 公 众 号 搜【 可可 找卡】,每天更新运营商官方高性价比套餐!帮你精准匹配适配流量方...