思路是:把tableview和cell都进行上下颠倒下,使用定时器,往数据源中增加数据,每个数据都是插入到第一个位置
#import "ViewController.h"
#import "customTableViewDataSource.h"
#import "CustomTableViewCell.h"
static const NSInteger maxCell = 7;
@interface ViewController ()
@property (nonatomic,strong)UITableView * myTableView;
@property (nonatomic,strong)customTableViewDataSource * dataSource;
@property (nonatomic,strong)NSMutableArray * textArr;
@end
@implementation ViewController
-(customTableViewDataSource *)dataSource{
if (!_dataSource) {
_dataSource = [[customTableViewDataSource alloc]init];
_dataSource.isRow = NO;
}
return _dataSource;
}
- (NSMutableArray *)textArr
{
if (!_textArr) {
_textArr = [[NSMutableArray alloc] init];
}
return _textArr;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createTableView];
[self setUpDataSource];
}
-(void)createTableView{
UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectMake(100, 100, 200, 200) style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor redColor];
tableView.dataSource = self.dataSource;
tableView.delegate = self.dataSource;
self.dataSource.rowHeight= 30;
// tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:tableView];
tableView.transform = CGAffineTransformMakeScale(1, -1);
self.myTableView = tableView;
[self tableViewConfig];
}
- (void)tableViewConfig{
__weak typeof(self) weakSelf = self;
self.dataSource.tableViewCell = ^UITableViewCell *(id _Nonnull model) {
CustomTableViewCell * cell = [CustomTableViewCell createChatTableViewCellWithTableView:weakSelf.myTableView];
// cell.textLabel.text = model;
[cell fillCellWithString:model];
return cell;
};
}
/**
* 造点假数据
*/
- (void)setUpDataSource
{
for (NSInteger i = 0; i < 6; i++) {
NSMutableString *text = [[NSMutableString alloc] initWithString:@"哈"];
for (NSInteger j = 0; j <= i; j++) {
[text appendFormat:@"哈"];
}
[text appendFormat:@"%zd", i];
[self.textArr addObject:text];
}
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(sendMessage) userInfo:nil repeats:YES];
}
- (void)sendMessage
{
NSString *text = self.textArr[arc4random() % 6];
[self.dataSource.dataSourceArray insertObject:text atIndex:0];
[self.myTableView insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationTop];
if (self.dataSource.dataSourceArray.count > maxCell) {
[self.dataSource.dataSourceArray removeLastObject];
[self.myTableView deleteSections:[NSIndexSet indexSetWithIndex:self.dataSource.dataSourceArray.count] withRowAnimation:UITableViewRowAnimationNone];
}
}
@end
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
typedef UITableViewCell *(^tableViewCellBlock)(id model);
@interface customTableViewDataSource : NSObject<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray * dataSourceArray;
@property (nonatomic,copy)tableViewCellBlock tableViewCell;
/**
* 设置是否是按组还是row yes : row no : section 默认是yes
*/
@property (nonatomic, assign) BOOL isRow;
/**
* 设置tableviewcell的高度
*/
@property (nonatomic, assign) CGFloat rowHeight;
/**
* 设置tableview头部view 默认无
*/
@property (nonatomic, strong) UIView *headView;
@end
NS_ASSUME_NONNULL_END
#import "customTableViewDataSource.h"
@interface customTableViewDataSource ()
@end
@implementation customTableViewDataSource
-(instancetype)init{
self = [super init];
if (self) {
self.dataSourceArray = [NSMutableArray array];
self.isRow = YES;
}
return self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (self.isRow) {
return 1;
} else {
return self.dataSourceArray.count;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.isRow) {
return self.dataSourceArray.count;
} else {
return 1;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.rowHeight;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (self.headView) {
return self.headView;
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * model = self.dataSourceArray[indexPath.section];
UITableViewCell *cell = nil;
if (self.tableViewCell) {
cell = self.tableViewCell(model);
} else {
cell = [[UITableViewCell alloc] init];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (self.headView) {
return self.headView.bounds.size.height;
}
return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 5.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [[UIView alloc] init];
}
@end
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface CustomTableViewCell : UITableViewCell
+ (instancetype)createChatTableViewCellWithTableView:(UITableView *)tableView;
-(void)fillCellWithString:(NSString *)sting;
@end
NS_ASSUME_NONNULL_END
#import "CustomTableViewCell.h"
@interface CustomTableViewCell ()
/**
* 此处可以用lable直接使用
*/
@property (nonatomic, strong) UIView *mainView;
@end
@implementation CustomTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
+ (instancetype)createChatTableViewCellWithTableView:(UITableView *)tableView{
CustomTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
if (!cell) {
cell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PDChatTableViewCell"];
}
return cell;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.textLabel.font = [UIFont systemFontOfSize:14];
self.textLabel.transform = CGAffineTransformMakeScale(1, -1);
[self setUpCellUI];
}
return self;
}
-(void)setUpCellUI{
UIView *cornerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
cornerView.backgroundColor = [UIColor whiteColor];
cornerView.layer.cornerRadius = 3.0;
cornerView.layer.masksToBounds = YES;
[self.contentView insertSubview:cornerView belowSubview:self.textLabel];
self.mainView = cornerView;
}
-(void)fillCellWithString:(NSString *)str{
self.mainView.frame = CGRectMake(0, 0, 100, 30);
self.textLabel.text = str;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
网友评论