UITableView的基本使用 cell的單選和多選
<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
//用來記錄我們點選的indexpath
@property(nonatomic,assign)NSIndexPath *isSelected;
//建立tableview
_tableView=[[UITableViewalloc]initWithFrame:CGRectMake(0, 238, WEIDTH, HEIGHT-235-67) style:UITableViewStylePlain
_tableView.delegate=self;
_tableView.dataSource=self;
_tableView.tableFooterView=[[UIViewalloc]initWithFrame:CGRectZero];
_tableView.rowHeight=80;
[self.viewaddSubview:_tableView];
//註冊我們自定義的cell
[self.tableViewregisterClass:[StatusTableViewCellclass] forCellReuseIdentifier:NSStringFromClass
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
StatusTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([StatusTableViewCellclass])];
if (indexPath.row==0) {
cell.nameLabel.text=@"";
cell.dataLabel.text=@"";
cell.timeLabel.text=@"";
cell.numberLabel.text=@"";
cell.bgView.backgroundColor=[UIColorcolorWithRed:158/255.0green:222/255.0blue:89/255.0alpha:1];
cell.HeadimageView.image=[UIImageimageNamed:@"turang"];
}
if (indexPath.row==1) {
cell.nameLabel.text=@"";
cell.dataLabel.text=@"";
cell.timeLabel.text=@"";
cell.numberLabel.text=@"";
cell.bgView.backgroundColor=[UIColorcolorWithRed:245/255.0green:113/255.0blue:112/255.0alpha:1];
cell.HeadimageView.image=[UIImageimageNamed:@"yanfen"];
}
if (indexPath.row==2) {
cell.nameLabel.text=@"";
cell.dataLabel.text=@"";
cell.timeLabel.text=@"";
cell.numberLabel.text=@"";
cell.bgView.backgroundColor=[UIColorcolorWithRed:122/255.0green:179/255.0blue:249/255.0alpha:1];
cell.HeadimageView.image=[UIImageimageNamed:@"shuiwexi"];
}
if (indexPath.row==3) {
cell.nameLabel.text=@"";
cell.dataLabel.text=@"";
cell.timeLabel.text=@"";
cell.numberLabel.text=@"";
cell.bgView.backgroundColor=[UIColorcolorWithRed:251/255.0green:175/255.0blue:82/255.0alpha:1];
cell.HeadimageView.image=[UIImageimageNamed:@"ph"];
}
return cell;
}
//單選
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
//找cell主要可以用來單選
StatusTableViewCell *celled=[tableView cellForRowAtIndexPath:_isSelected];
celled.accessoryType=UITableViewCellAccessoryNone;
StatusTableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType=UITableViewCellAccessoryCheckmark;
self.isSelected=indexPath;
//加上下面的就可以一個都不選,僅僅上面的就會始終有一個
// if (_isSelected==indexPath) {
//
// celled.accessoryType=UITableViewCellAccessoryNone;
// }else{
//
// cell.accessoryType=UITableViewCellAccessoryCheckmark;
// }
}
//多選
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// [tableView deselectRowAtIndexPath:indexPath animated:NO];
//
// StatusTableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
// if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { //如果為選中狀態
// cell.accessoryType = UITableViewCellAccessoryNone; //切換為未選中
// [_selectIndexs removeObject:indexPath]; //資料移除
// }else { //未選中
// cell.accessoryType = UITableViewCellAccessoryCheckmark; //切換為選中
// [_selectIndexs addObject:indexPath]; //新增索引資料到陣列
// } NSLog(@"indexPath==%@",_isSelected);
//}