You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

100 lines
3.7 KiB

//
// SwitchDeviceView.m
// neutralWatch
//
// Created by WeiChaoZheng on 2017/12/30.
// Copyright © 2017年 xTT. All rights reserved.
//
#import "SwitchDeviceView.h"
#import "SwitchDeviceCell.h"
#import "Device.h"
@interface SwitchDeviceView()
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,assign) CGFloat tableViewHeight;
@end
@implementation SwitchDeviceView
-(instancetype)initWithFrame:(CGRect)frame ShowObjects:(NSArray<id> *)objects{
self= [super initWithFrame:frame];// 先调用父类的initWithFrame方法
if (self) {
self.objects = [objects mutableCopy];
// CGFloat tableViewHeight = objects.count*45+objects.count-1;
// if(tableViewHeight+10 > frame.size.height){
// tableViewHeight = frame.size.height-10;
// }
_tableViewHeight = frame.size.height;
UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, _tableViewHeight)];
leftImageView.image = [self imageCapWithName:@"img_watch_arrow_left"];
UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, _tableViewHeight)];
rightImageView.image = [self imageCapWithName:@"img_watch_arrow_right"];
[self addSubview:leftImageView];
[self addSubview:rightImageView];
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, frame.size.width, _tableViewHeight-10) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = [UIColor clearColor];
[self.tableView registerNib:[UINib nibWithNibName:@"SwitchDeviceCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"SwitchDeviceCellID"];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.bounces = NO;
[self addSubview:self.tableView];
}
return self;
}
-(UIImage*) imageCapWithName:(NSString*)name {
UIImage * image = [UIImage imageNamed:name];
if(image){
int h = (int)image.size.height*0.5; //从 5/10 处切割
int w = (int)image.size.width*0.5; //从 5/10 处切割
UIImage *newImage = [image stretchableImageWithLeftCapWidth:w topCapHeight:h];
return newImage;
}
return nil;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.objects.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return (_tableViewHeight-10)/self.objects.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
SwitchDeviceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SwitchDeviceCellID" forIndexPath:indexPath];
if([self.objects[indexPath.row] isKindOfClass:[Device class]]){
Device *temp = self.objects[indexPath.row];
cell.titleLabel.text = temp.name;
}else{
cell.titleLabel.text = self.objects[indexPath.row];
}
cell.backgroundColor = [UIColor clearColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.row == self.objects.count-1) {
[cell.lineView setHidden:YES];
}else{
[cell.lineView setHidden:NO];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if(self.selectBlock){
self.selectBlock(self.objects[indexPath.row]);
}
}
@end