iOS實(shí)現(xiàn)可以縱向橫向滑動(dòng)的表格實(shí)例代碼
本文主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)可以縱向橫向滑動(dòng)的表格的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面來(lái)一起看看詳細(xì)的介紹:
效果圖

這個(gè)效果是今天公司項(xiàng)目里面遇上的,也是第一次遇見(jiàn)這種需求,所以記錄下來(lái),效果如上圖。需求主要是可以實(shí)現(xiàn)上下的滑動(dòng),并且同時(shí)最左側(cè)的“線路名稱”這一列在向左滑動(dòng)的時(shí)候是不能跟隨滾動(dòng)的。這個(gè)功能主要是實(shí)現(xiàn)用戶可以方便查看關(guān)于一下難以看全的列表數(shù)據(jù)。下面說(shuō)一下思路。
代碼大體思路
由上面的GIF圖和基本需求描述我們第一個(gè)想到的東西就是萬(wàn)能的tableview,沒(méi)錯(cuò),這個(gè)功能的完成當(dāng)然離不開tableview,那么tableview應(yīng)該怎樣發(fā)揮它的功力呢,左右側(cè)的信息需要對(duì)稱,所以在這里我使用了兩個(gè)tableview,也就是最左側(cè)線路名稱這一列是一個(gè)tableview,右側(cè)的粉紅色字體這些行是一個(gè)tableview。上下滑動(dòng)兩者關(guān)聯(lián)是使用scrollview完成的。那接下來(lái)就結(jié)合代碼簡(jiǎn)單說(shuō)一下,也方便我以后回頭看,哈哈哈。
代碼解析
1、這是需要的原材料,每個(gè)變量都有注釋它的功能了,一眼懂。titleTableView是最左側(cè)的線路名稱這一列。infoTableView是粉紅色字體這些。contentView是titleTableView和最上方(除了“線路名稱”)這一列內(nèi)容的superView。
@property (nonatomic, strong) UITableView *titleTableView;//標(biāo)題TableView
@property (nonatomic, strong) UITableView *infoTableView;//內(nèi)容TableView
@property (nonatomic, strong) UIScrollView *contentView;//內(nèi)容容器
@property (nonatomic, strong) NSArray *infoArr;//數(shù)組
@end
@implementation ViewController {
CGFloat _kOriginX;
CGFloat _kScreenWidth;
CGFloat _kScreenHeight;
}
2、這是所需要的數(shù)據(jù)配置,我把里面所有需要的數(shù)據(jù)都放在數(shù)組李典里面了。我比較懶。哈哈哈哈
- (void)configData {
_kOriginX = 120;
_kScreenWidth = self.view.frame.size.width;
_kScreenHeight = self.view.frame.size.height;
_infoArr = @[@{@"title":@"出團(tuán)日期", @"routeName":@"線路名稱一", @"time":@"2015/11/21", @"num":@"20", @"price":@"124.0", @"code":@"DAGSDSASA"},
@{@"title":@"余位", @"routeName":@"線路名稱二", @"time":@"2015/11/21", @"num":@"34", @"price":@"234", @"code":@"TAGDFASFAF"},
@{@"title":@"價(jià)格", @"routeName":@"線路名稱三", @"time":@"2015/11/21", @"num":@"12", @"price":@"634", @"code":@"GHGASDAS"},
@{@"title":@"團(tuán)代號(hào)", @"routeName":@"線路名稱四", @"time":@"2015/11/56", @"num":@"54", @"price":@"632", @"code":@"DAADSFAD"}];
}
3、分步來(lái)看,首先是頭部的,這個(gè)titleLabel是最左上角的“線路名稱”這四個(gè)字,contentView的配置,上面說(shuō)了這個(gè)contentView的作用的,從它的frame看出來(lái), _contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(_kOriginX, 0, _kScreenWidth - _kOriginX, _kScreenHeight)];它的x是_kOriginX也就是預(yù)留的最左側(cè)的空間。最上面的一列使用for循環(huán)創(chuàng)建出來(lái)的label。
//MARK:- 頭部視圖
- (void)configTableHeader {
UILabel *titleLabel = [self quickCreateLabelWithLeft:0 width:_kOriginX title:@"線路名稱"];
[self.view addSubview:titleLabel];
_contentView = [[UIScrollView alloc] initWithFrame:CGRectMake(_kOriginX, 0, _kScreenWidth - _kOriginX, _kScreenHeight)];
_contentView.delegate = self;
_contentView.showsVerticalScrollIndicator = NO;
_contentView.showsHorizontalScrollIndicator = NO;
_contentView.contentSize = CGSizeMake(400, _kScreenHeight);
_contentView.bounces = NO;
[self.view addSubview:_contentView];
for (int i = 0; i < _infoArr.count; i++) {
CGFloat x = i * 100;
UILabel *label = [self quickCreateLabelWithLeft:x width:100 title:[[_infoArr objectAtIndex: i] objectForKey:@"title"]];
label.textAlignment = NSTextAlignmentCenter;
[_contentView addSubview:label];
}
}
4、那接下來(lái)就是配置最左側(cè)那一欄和左側(cè)粉紅色字體那些行。也就這兩個(gè)tableview創(chuàng)建的。
//MARK:- 詳細(xì)內(nèi)容
- (void)configInfoView {
_titleTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, _kOriginX, _kScreenHeight) style:UITableViewStylePlain];
_titleTableView.dataSource = self;
_titleTableView.delegate = self;
_titleTableView.showsVerticalScrollIndicator = NO;
_titleTableView.showsHorizontalScrollIndicator = NO;
_titleTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:_titleTableView];
_infoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, 400, _kScreenHeight) style:UITableViewStylePlain];
_infoTableView.delegate = self;
_infoTableView.dataSource = self;
_infoTableView.showsVerticalScrollIndicator = NO;
_infoTableView.showsHorizontalScrollIndicator = NO;
_infoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[_contentView addSubview:_infoTableView];
}
5、這是tableview的代理方法實(shí)現(xiàn)。在cellForRowAtIndexPath這個(gè)代理方法中,將兩個(gè)tableview的cell分開來(lái)寫。
//MARK:- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _infoArr.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == _titleTableView) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"titleTable"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"titleTable"];
}
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.text = [[_infoArr objectAtIndex:indexPath.row] objectForKey:@"routeName"];
cell.textLabel.textColor = [UIColor lightGrayColor];
cell.textLabel.font = [UIFont systemFontOfSize:14];
if (indexPath.row%2 == 1) {
cell.backgroundColor = [UIColor colorWithRed:218/255.0 green:218/255.0 blue:218/255.0 alpha:1];
} else {
cell.backgroundColor = [UIColor whiteColor];
}
return cell;
} else {
NSString *ident = @"InfoCell";
InfoCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"InfoCell" owner:nil options:nil] lastObject];
}
if (indexPath.row%2 == 1) {
cell.backgroundColor = [UIColor colorWithRed:218/255.0 green:218/255.0 blue:218/255.0 alpha:1];
} else {
cell.backgroundColor = [UIColor whiteColor];
}
[cell setDataWithStr:[_infoArr objectAtIndex:indexPath.row]];
return cell;
}
}
//MARK:- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"選中了%@", [_infoArr[indexPath.row] objectForKey:@"routeName"]);
}
6、這個(gè)方法就是實(shí)現(xiàn)上下滑動(dòng)時(shí)候,左側(cè)和右側(cè)tableview聯(lián)動(dòng)的實(shí)現(xiàn)方法。
//MARK:- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == _titleTableView) {
[_infoTableView setContentOffset:CGPointMake(_infoTableView.contentOffset.x, _titleTableView.contentOffset.y)];
}
if (scrollView == _infoTableView) {
[_titleTableView setContentOffset:CGPointMake(0, _infoTableView.contentOffset.y)];
}
}
總結(jié)
啊,寫完感覺(jué)也是比較簡(jiǎn)單,就是基本方法的配合使用,當(dāng)時(shí)想的時(shí)候也是沒(méi)有能一下想出來(lái),還是自己基本功不好的原因吧。把這個(gè)效果的實(shí)現(xiàn)記錄在這里,也是為了提醒自己,也就是這個(gè)功能比較簡(jiǎn)單,但是再怎樣的功能都是靠最基本的東西堆砌的。思想很重要,但是最重要的還是去實(shí)現(xiàn),光想沒(méi)有用,人不是靠嘴活的。與君共勉。
代碼地址:https://github.com/irembeu/HorizontalSwipListView.git
本地下載地址:http://xiazai.jb51.net/201706/yuanma/ListTableView(jb51.net).rar
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)給各位iOS開發(fā)者們能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
iOS 縮小打包項(xiàng)目ipa大小的實(shí)現(xiàn)方法
下面小編就為大家分享一篇iOS 縮小打包項(xiàng)目ipa大小的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
IOS 調(diào)整內(nèi)存中的圖片大小實(shí)例詳解
這篇文章主要介紹了IOS 調(diào)整內(nèi)存中的圖片大小實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
iOS開發(fā)中視圖的下拉放大和上拉模糊的效果實(shí)現(xiàn)
這篇文章主要介紹了iOS開發(fā)中視圖的下拉放大和上拉模糊的效果實(shí)現(xiàn),本文以秘密App的效果作為示例進(jìn)行講解,需要的朋友可以參考下2015-09-09
iOS自定義UICollectionViewLayout實(shí)現(xiàn)瀑布流布局
這篇文章主要為大家詳細(xì)介紹了iOS自定義UICollectionViewLayout實(shí)現(xiàn)瀑布流布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
iOS開發(fā)之?dāng)?shù)字每隔3位用逗號(hào)分隔
以前在做電商app時(shí)經(jīng)常會(huì)針對(duì)稍大的金額展示出來(lái),需要每隔千位添加逗號(hào)便于用戶識(shí)別,下面通過(guò)本文給大家分享ios中數(shù)字每隔3位用逗號(hào)分隔的實(shí)例代碼,需要的朋友參考下吧2017-09-09

