平時(shí)常用的一些控件,閑的時(shí)候單獨(dú)封裝出來(lái)
封裝彈出選擇視圖的tableviewcell OC和Swift兩個(gè)版本 ,demo地址點(diǎn)這里
OC:
選擇日期DatePickerCell的使用:
DatePickerCell *cell = [DatePickerCell dequeueReusableWithTableView:tableView];
cell.selectDatePickerBlock = ^(NSDate *date) {
NSLog(@"%@",date);
};
彈出點(diǎn)擊選擇ClickPickerCell:
ClickPickerCell *cell = [ClickPickerCell dequeueReusableWithTableView:tableView dataSource:@[@"男",@"女",@"ladyBoy"]];
__weak typeof (cell)weakCell = cell;
cell.selectClickPickerBlock = ^(NSInteger index, NSString *str) {
[weakCell setDetailTitle:str];
};
滑動(dòng)選擇PickerViewCell:
PickerViewCell *cell = [PickerViewCell dequeueReusableWithTableView:tableView withDataSource:@[@[@"a",@"b",@"c",@"d"],@[@"1",@"2",@"3",@"4"],@[@"??",@"??",@"??",@"??"]]];
__weak typeof (cell)weakCell = cell;
cell.selectPickerViewBlock = ^(NSArray *selectArray){
[weakCell setDetailTitle:[selectArray componentsJoinedByString:@""]];
};
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[cell becomeFirstResponder];
}
Swift:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = MKDatePickerCell.dequeueReusable(WithTableView: tableView)
cell.datePickerSelectBlock = {
print($0)
}
return cell
}else if indexPath.row == 1 {
let cell = MKClickPickerCell.dequeueReusable(WithTableView: tableView, dataSource: array1)
cell.selectClickPickerBlock = {
print("index = \($0),str = \($1)")
}
return cell
}else{
let cell = MKPickerViewCell.dequeueReusable(WithTableView: tableView, dataSource: array2)
cell.selectPickerViewBlock = { array in
print(array.joined())
}
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let celll = tableView.cellForRow(at: indexPath)
guard let cell = celll else { return }
if cell.isFirstResponder {
cell.resignFirstResponder()
}else{
cell.becomeFirstResponder()
}
}