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

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

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

2019-11-09 14:14:43
字體:
供稿:網(wǎng)友

1.問題提出

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

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

當(dāng)時(shí)這樣設(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;}

我理想的界面是:                              

    

        

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

  

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

2.解決方法

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

變量:

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

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

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

情景:

假設(shè)一頁顯示20個(gè)表情,1個(gè)刪除按鈕。現(xiàn)在有兩個(gè)表情組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:有兩個(gè)表情組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)個(gè)贊哦O(∩_∩)O哈哈哈~)

仿微信表情鍵盤


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 来宾市| 甘谷县| 远安县| 那曲县| 秭归县| 正镶白旗| 太和县| 沙坪坝区| 蕲春县| 闻喜县| 井冈山市| 苍溪县| 定安县| 嘉禾县| 宁河县| 南川市| 漾濞| 沂水县| 遂昌县| 靖安县| 阿合奇县| 大竹县| 亳州市| 蓬安县| 治县。| 宁陕县| 玛曲县| 潜江市| 晋宁县| 瓦房店市| 南部县| 随州市| 天长市| 盱眙县| 招远市| 准格尔旗| 彰武县| 海原县| 西乌珠穆沁旗| 栾川县| 尤溪县|