單元格左側附帶選擇按鈕

這裏寫圖片描述
這是之前足球兄弟項目裏實現的一個效果,聯繫人左側有一個附帶的選擇按鈕,點擊可選中,再次點擊取消選中。很簡單的一個效果,由於當時工作經驗不做,第一想法是在單元格的左側添加按鈕,設置按鈕在選中和未選中狀態的圖片。後來發現一個非常簡單的系統自帶的方法,記錄如下:
創建一個單元格 設置單元格爲可編輯
創建tableView:
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[tableView setEditing:YES];
[self.view addSubview:tableView];
設置tableView返回的單元格個數
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
下面這個方法就是我們實現以上效果的核心
-(UITableViewCellEditingStyle)tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath
{
return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
}
編輯單元格
-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @”cellId”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
cell.imageView.image = [UIImage imageNamed:@”1”];
return cell;
}
到這裏,我們一個簡單的demo就完成了,運行效果如下圖所示:
這裏寫圖片描述
努力學習,獨立鑽研,一些看似棘手的問題,其實非常簡單。讓我們一起進步吧!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章