iOS - UITableView 单选功能实现
#import <UIKit/UIKit.h> @interface TestCell : UITableViewCell
@property(nonatomic,copy)NSString *content;
/**
* 标记行是否被选中
*/
@property(nonatomic,assign)BOOL isChecked;
@end
#import "TestCell.h"
@interface TestCell()
@property (weak, nonatomic) IBOutlet UIImageView *mark_ImageView;
@property (weak, nonatomic) IBOutlet UILabel *firstLabel; @end @implementation TestCell - (void)awakeFromNib
{
self.mark_ImageView.hidden = YES;
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; }
-(void)setContent:(NSString *)content
{
if (_content!=content) {
_firstLabel.text=content;
} }
/**
* 这里只是用到了一张图所以通过来imageview的hidden来显示是否选中
*
* @param isChecked
*/
- (void)setIsChecked:(BOOL)isChecked
{
_isChecked = isChecked;
//选中
if (_isChecked) {
self.mark_ImageView.hidden = NO;
self.mark_ImageView.image = [UIImage imageNamed:@"mark_select"];
}
else
{
self.mark_ImageView.hidden = YES;
} } @end #import "ViewController.h"
#import "TestCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> {
NSIndexPath *selectIndexPath; } @property(nonatomic,strong)NSMutableArray *dataArray; /**
* 保存选中行的对应的数据
*/
@property(nonatomic,strong)NSMutableArray *markArray; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; for (NSInteger i=0; i<20; i++) {
[self.dataArray addObject:[NSString stringWithFormat:@"选中第%ld行",i]];
} } - (NSMutableArray *)dataArray{ if (!_dataArray) {
_dataArray =[NSMutableArray array];
}
return _dataArray; } - (NSMutableArray *)markArray{ if (!_markArray) {
_markArray =[NSMutableArray array];
}
return _markArray; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataArray.count;
}
static NSString *cellIdentifier = @"TestCell";
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ TestCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
cell.content= self.dataArray[indexPath.row]; if (selectIndexPath==indexPath) {
cell.isChecked = YES;
}
else{
cell.isChecked = NO;
} return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44.0f;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//未选中
TestCell *noCheckedCell = (TestCell *)[tableView cellForRowAtIndexPath:selectIndexPath];
noCheckedCell.isChecked = NO;
selectIndexPath = indexPath; //选中
TestCell *checkedCell = (TestCell *)[tableView cellForRowAtIndexPath:indexPath];
checkedCell.isChecked = YES; [tableView deselectRowAtIndexPath:indexPath animated:YES];
//刷新指定的行
NSIndexPath *indexPath_Row=[NSIndexPath indexPathForRow:indexPath.row inSection:0];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath_Row,nil] withRowAnimation:UITableViewRowAnimationNone]; NSString *checkString = self.dataArray[indexPath.row];
if ([self.markArray containsObject:checkString]) {
[self.markArray removeObject:checkString];
}
else{
[self.markArray removeAllObjects];
[self.markArray addObject:checkString];
} NSLog(@"self.checkArray =%@",self.markArray); }

iOS - UITableView 单选功能实现的更多相关文章
- IOS UITableView删除功能
UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,比如购物车等.删除功能可以直接使用系统自带的删除功能,当横向轻扫cell时,右侧出现红色的删除按钮,点击删除当前cell. ...
- IOS UITableView NSIndexPath属性讲解
IOS UITableView NSIndexPath属性讲解 查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和deleg ...
- iOS UITableView划动删除的实现
标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...
- 自定义GrildView实现单选功能
首先看实现功能截图,这是一个自定义Dialog,并且里面内容由GrildView 绑定数据源,实现类似单选功能. 首先自定义Dialog,绑定数据源 自定义Dialog弹出框大小方法 最主要实现的就是 ...
- iOS 按钮倒计时功能
iOS 按钮倒计时功能, 建议把按钮换成label,这样会避免读秒时闪烁 __block ; __block UIButton *verifybutton = _GetverificationBtn; ...
- C# winform项目中ListView控件使用CheckBoxes属性实现单选功能
C# winform项目中ListView控件使用CheckBoxes属性实现单选功能 在做项目时需要使用ListView控件的CheckBoxes属性显示,还要在点击行时自动选中CheckBoxes ...
- ASP.NET js控制treeview中的checkbox实现单选功能
ASP.NET js控制treeview中的checkbox实现单选功能 function OnTreeNodeChecked() { var element = window.event.srcEl ...
- 提高 iOS App 通知功能启用率的三个策略
我们都知道推送通知在 App 运营中的作用巨大.但是,很多用户却并不买帐,App 第一次启动提示是否「启用推送通知」时,他们直接选择了「否」. 是的,最近我本人就转变成了这样的人 - 认真地评估每个应 ...
- iView的tree组件实现单选功能
iView中的树组件有复选框可以多选,但是目前还没有提供单选框的模式,不显示复选框可以提供高亮单选的模式,但是再次点击就被取消了,没有实现真正的单选: tree 的属性配置中 multiple 是否支 ...
随机推荐
- PID控制温度
总所周知,PID算法是个很经典的东西.而做自平衡小车,飞行器PID是一个必须翻过的坎.因此本节我们来好好讲解一下PID,根据我在学习中的体会,力求通俗易懂.并举出PID的形象例子来帮助理解PID.一. ...
- COJ 1351 Tree Counting 动态规划
题目大意是: 给定一个n,k,表示树上共有n个节点,每个节点最多有k个叶子,问一共多少种摆法,答案对1000000007取模 这里定义一个dp[i]表示 i 个节点对应有多少种方法 f[i][j] 表 ...
- node.js 核心http模块,起一个服务器,返回一个页面
let http=require("http"); //引入核心http模块 let fs=require("fs"); let mime={ '.js':'a ...
- Java中原始数据类型存放位置理解
原始数据类型的变量存放在栈还是堆,应该由上下文去决定. 如下所示的局部方法中,定义了本地变量a,且为原始数据类型,所以存放在栈中. public void func(){ int a = 3; } 再 ...
- 【CV知识学习】【转】beyond Bags of features for rec scenen categories。基于词袋模型改进的自然场景识别方法
原博文地址:http://www.cnblogs.com/nobadfish/articles/5244637.html 原论文名叫Byeond bags of features:Spatial Py ...
- Java序列化之readObjectNoData、readResolve方法
Java序列化之readObjectNoData.readResolve方法 学习了:http://vyloy.iteye.com/blog/1240663 readResolve方法会在Object ...
- iOS8開始默认语言有变化
[问题] 測试组发现APP在iOS8及以上系统设备上,语言设置为我们不支持的小语种时.APP没有使用默认的英文,而是选择了上一次设置的语言. [分析] 经过研究发现,在iOS8系统開始,在设备语言设置 ...
- struts2前端页面读取Clob/BLOB
在通过Struts2标签显示对象的Clob属性值的时候.显示的并非CLOB或者BLOB的内容,而是显示的toString方法的值 比如我在实体中的注解为: @Lob @Column(name = &q ...
- inline-block元素因基线对齐而造成上浮的问题
假设我需要实现将三个块级元素并排对齐的如下效果: 代码如下: <!DOCTYPE html> <html lang="en-US"> <head> ...
- HDU 5311 Sequence
Hidden String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...