封装弹出选择试图的tableviewcell OC和Swift两个版本
DatePickerCell *cell = [DatePickerCell dequeueReusableWithTableView:tableView];
cell.selectDatePickerBlock = ^(NSDate *date) {
NSLog(@"%@",date);
};
ClickPickerCell *cell = [ClickPickerCell dequeueReusableWithTableView:tableView dataSource:@[@"男",@"女",@"ladyBoy"]];
__weak typeof (cell)weakCell = cell;
cell.selectClickPickerBlock = ^(NSInteger index, NSString *str) {
[weakCell setDetailTitle:str];
};
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];
}
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()
}
}