iOS聊天界面cell从下至上滚动

来源:互联网 发布:技术支持 盘古网络 编辑:程序博客网 时间:2024/06/12 22:00

import "ChatView.h"

import "ChatCell.h"

@interface ChatView ()

@property (nonatomic, strong) NSMutableArray dataSoure;
@property (nonatomic, strong) UITableView
tableView;

@end

@implementation ChatView

  • (instancetype)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {

      self.backgroundColor = [UIColor clearColor];  self.dataSoure = [NSMutableArray array];  self.tableView = [[UITableView alloc]initWithFrame:self.bounds];  _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;  _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;  _tableView.showsVerticalScrollIndicator = NO;  _tableView.backgroundColor = [UIColor clearColor];  _tableView.delegate = self;  _tableView.dataSource = self;

    _tableView.transform = CGAffineTransformMakeScale(1, -1);

      [self addSubview:self.tableView];

    }
    return self;
    }

  • (void)reloadData
    {
    [self.tableView reloadData];
    [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
    }

pragma mark - UITableViewDelegate && UITableViewDataSource

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return self.dataSoure.count;
    }

  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath)indexPath
    {
    static NSString
    identifier = @"LiveChatCell";
    ChatCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {

      cell = [[ChatCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }
    cell.contentView.transform = CGAffineTransformMakeScale(1, -1);
    NSInteger index = self.dataSoure.count-1-indexPath.row;
    cell.item = self.dataSoure[index];//cell填充数据
    return cell;
    }

  • (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath)indexPath
    {
    NSInteger index = self.dataSoure.count-1-indexPath.row;
    return [ChatCell cellHeightWithItem:self.dataSoure[index] tableView:tableView];
    }

pragma mark - UIScrollViewDelegete

//开始拖拽

  • (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(scrollStopTime) object:nil];
    }

//结束拖拽

  • (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
    [self performSelector:@selector(scrollStopTime) withObject:nil afterDelay:3.0f];
    }

  • (void)scrollStopTime
    {
    [self.tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
    }
    @end

1 0
原创粉丝点击