程序中有很多時候會用到UITableView的選擇,多選或者單選,單選要簡單一些,下面簡單記錄一個示例。
要實現如下圖的效果:點擊前面幾行,黑點隨之移動,當點擊其他時候,會出來一個輸入框。
實現方式有很多種,這裏就簡單的以一種最普通的方式來實現,步驟:
1.實現基本的tableView列表及實現基本的代理方法。
2.定義全局UITextView,實現顯示隱藏它的方法。
3.實現表單元格點擊選中樣式
4.在cellForRowAtIndexPath中根據選中狀態設置樣式等。
部分代碼如下示例:
#import "TestViewController.h"
@interface TestViewController ()<
UITableViewDataSource,
UITableViewDelegate,
UITextViewDelegate>{
UITextView *_reportView;
NSArray *_titleArr;
}
// 初始化
- (void)initSubViews;
// 加載列表視圖
- (void)initTableView;
// 加載輸入視圖
- (void)setReportViewShow:(BOOL)isHidden;
@end
@implementation BaoReportViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self initSubViews];
}
return self;
}
- (void)viewDidLoad{
[super viewDidLoad];
[self initTableView];
}
- (void)initSubViews{
self.rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成"
style:UIBarButtonItemStylePlain
target:self
action:@selector(finishInputReportText:)];
_titleArr = @[@"欺詐騙錢",@"色情暴力",@"廣告騷擾",@"其他"];
// 初始化選中第一個cell
_selectedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
_checkMark = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth-70, 10, 18, 22)];
}
// 加載子視圖
- (void)initTableView{
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, _scrollView.width, kReportTypeTableHeight)
style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.scrollEnabled= NO;
[self.view addSubview:_tableView];
}
// 加載輸入框
- (void)setReportViewShow:(BOOL)show{
if (show) {
_reportView.frame = CGRectMake(10, _tableView.bottom, _tableView.width-20, kReportViewHeight);
}else {
_reportView.frame = CGRectZero;
}
}
// 加載按鈕
- (void)loadSendReportBtn{
UIButton *sendReportBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
sendReportBtn.frame = CGRectMake(0, _scrollView.height -kMarginTop- kSendReportBtnHeight, _scrollView.width, kSendReportBtnHeight);
sendReportBtn.tintColor = [UIColor whiteColor];
sendReportBtn.backgroundColor = [UIColor blackColor];
[sendReportBtn setTitle:@"確定" forState:UIControlStateNormal];
[sendReportBtn addTarget:self
action:@selector(sendReportAction:)
forControlEvents:UIControlEventTouchUpInside];
[_scrollView addSubview:sendReportBtn];
}
#pragma mark - TableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [_titleArr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
int row = indexPath.row;
static NSString *cellIdentifier = @"BaoPicCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
//cell.selectionStyle = UITableViewCellSelectionStyleNone
cell.textLabel.text = [_titleArr objectAtIndex:row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont fontWithName:nil size:15.0f];
UIImageView *underMark = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth-70, 10, 20, 20)];
[cell.contentView addSubview:underMark];
underMark.image = [UIImage imageNamed:@"yuandian"];
}
// 處理選中
if (self.selectedIndexPath.row == indexPath.row){
_checkMark.image = [UIImage imageNamed:@"bpheidian"];
[cell.contentView addSubview:_checkMark];
}
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 50)];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 150, 50)];
titleLabel.text = @"舉報類型";
titleLabel.font = [UIFont fontWithName:nil size:15.0f];
titleLabel.textColor = [UIColor grayColor];
[sectionHeaderView addSubview:titleLabel];
return sectionHeaderView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
_checkMark.image = [UIImage imageNamed:@"bpheidian"];
[cell.contentView addSubview:_checkMark];
self.selectedIndexPath = indexPath;
if (indexPath.row == 3) {
[self setReportViewShow:YES];
}else{
[self setReportViewShow:NO];
[self.view endEditing:YES];
self.navigationItem.rightBarButtonItem = nil;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 45.0f;
}
#pragma mark - TextViewDelegate method
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
return YES;
}
@end
上面的代碼可能並不能運行,還少鍵盤事件處理等等,這裏僅是爲了說明單選樣式及處理方式,如有錯誤,敬請諒解。