国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS- UICollectionView實現(xiàn)表情數(shù)據(jù)橫排橫滾動

2019-11-09 15:17:02
字體:
供稿:網(wǎng)友

1.問題提出

        在開發(fā)即時通訊項目時會遇到一個需求,那就是開發(fā)一個表情鍵盤。這時候?qū)崿F(xiàn)的方法有好多種,大部分人會考慮用UIScrollView實現(xiàn),但是當(dāng)表情有200個的時候,UIScrollView的button難道就要創(chuàng)建200個么?考慮到按鈕重用的,自然想到UICollectionView。想實現(xiàn)表情數(shù)據(jù)橫排橫滾動,可能會遇到設(shè)置cell的數(shù)據(jù)源的坑。

筆者數(shù)據(jù)源文件.plist長這樣:

當(dāng)時這樣設(shè)置過:

    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; //橫排滾動    layout.itemSize = CGSizeMake(itemWidth, kOneItemHeight);- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    YHExPRessionAddCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];    NSLog(@"%ld",(long)indexPath.item);    if (indexPath.item < self.dataArray.count) {        cell.model = self.dataArray[indexPath.item];    }    return cell;}

我理想的界面是:                              

    

        

 但實際運(yùn)行的界面:           

  

   仔細(xì)想想,就是cellForItemAtIndexPath設(shè)置錯誤。那么如何正確地實現(xiàn)表情數(shù)據(jù)橫排橫滾動呢,請看下文。

2.解決方法

一:獲取數(shù)據(jù)

變量:

kOnePageCount:一頁顯示的表情數(shù)量

emoticonGroupPageIndexs:各表情組起始頁下標(biāo)數(shù)組

emoticonGroupTotalPageCount:表情組總頁數(shù)

情景:

假設(shè)一頁顯示20個表情,1個刪除按鈕?,F(xiàn)在有兩個表情組A,B.其中A有6頁,B有2頁

kOnePageCount = 20;

emoticonGroupPageIndexs = @[0,6];

emoticonGroupTotalPageCount = 8;

算法:

    _emoticonGroups = [YHExpressionHelper emoticonGroups];   //獲取各表情組起始頁下標(biāo)數(shù)組    NSMutableArray *indexs = [NSMutableArray new];    NSUInteger index = 0;    for (YHEmoticonGroup *group in _emoticonGroups) {        [indexs addObject:@(index)];        NSUInteger count = ceil(group.emoticons.count / (float)kOnePageCount);        if (count == 0) count = 1;        index += count;    }    _emoticonGroupPageIndexs = indexs;       //表情組總頁數(shù)    NSMutableArray *pageCounts = [NSMutableArray new];    _emoticonGroupTotalPageCount = 0;    for (YHEmoticonGroup *group in _emoticonGroups) {        NSUInteger pageCount = ceil(group.emoticons.count / (float)kOnePageCount);        if (pageCount == 0) pageCount = 1;        [pageCounts addObject:@(pageCount)];        _emoticonGroupTotalPageCount += pageCount;    }    _emoticonGroupPageCounts = pageCounts;

二:設(shè)置UICollectionView 數(shù)據(jù)源

//表情組總頁數(shù) (eg:有兩個表情組A,B.其中A有6頁,B有2頁。則_emoticonGroupTotalPageCount = 8)- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {    return _emoticonGroupTotalPageCount;}//一頁顯示的表情數(shù)量- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {    return kOnePageCount + 1; // “1”是刪除按鈕}//設(shè)置Cell- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {        YHEmoticonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];    if (indexPath.row == kOnePageCount) {        cell.isDelete = YES;        cell.emoticon = nil;    } else {        cell.isDelete = NO;        cell.emoticon = [self _emoticonForIndexPath:indexPath];    }    return cell;}- (YHEmoticon *)_emoticonForIndexPath:(NSIndexPath *)indexPath {    NSUInteger section = indexPath.section;    for (NSInteger i = _emoticonGroupPageIndexs.count - 1; i >= 0; i--) {        NSNumber *pageIndex = _emoticonGroupPageIndexs[i];        if (section >= pageIndex.unsignedIntegerValue) {            YHEmoticonGroup *group = _emoticonGroups[i];            NSUInteger page = section - pageIndex.unsignedIntegerValue;            NSUInteger index = page * kOnePageCount + indexPath.row;                        // transpose line/row            NSUInteger ip = index / kOnePageCount;            NSUInteger ii = index % kOnePageCount;            NSUInteger reIndex = (ii % 3) * 7 + (ii / 3);            index = reIndex + ip * kOnePageCount;                        if (index < group.emoticons.count) {                return group.emoticons[index];            } else {                return nil;            }        }    }    return nil;}DuangDuang,成功運(yùn)行并顯示出我想要的界面。

另外可以參考我的Demo:(喜歡的點(diǎn)個贊哦O(∩_∩)O哈哈哈~)

仿微信表情鍵盤


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宁武县| 赣榆县| 梨树县| 汉源县| 时尚| 青铜峡市| 鄱阳县| 防城港市| 襄樊市| 德化县| 南汇区| 大足县| 贺州市| 喀喇沁旗| 同仁县| 岳阳县| 永清县| 曲周县| 阿巴嘎旗| 龙江县| 新兴县| 广西| 团风县| 石门县| 白沙| 山东省| 孝感市| 古丈县| 新兴县| 如东县| 清镇市| 北碚区| 漳平市| 扬州市| 韶关市| 南康市| 永吉县| 太湖县| 阳高县| 新源县| 高淳县|