#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 单选功能实现的更多相关文章

  1. IOS UITableView删除功能

    UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,比如购物车等.删除功能可以直接使用系统自带的删除功能,当横向轻扫cell时,右侧出现红色的删除按钮,点击删除当前cell. ...

  2. IOS UITableView NSIndexPath属性讲解

    IOS UITableView NSIndexPath属性讲解   查看UITableView的帮助文档我们会注意到UITableView有两个Delegate分别为:dataSource和deleg ...

  3. iOS UITableView划动删除的实现

    标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...

  4. 自定义GrildView实现单选功能

    首先看实现功能截图,这是一个自定义Dialog,并且里面内容由GrildView 绑定数据源,实现类似单选功能. 首先自定义Dialog,绑定数据源 自定义Dialog弹出框大小方法 最主要实现的就是 ...

  5. iOS 按钮倒计时功能

    iOS 按钮倒计时功能, 建议把按钮换成label,这样会避免读秒时闪烁 __block ; __block UIButton *verifybutton = _GetverificationBtn; ...

  6. C# winform项目中ListView控件使用CheckBoxes属性实现单选功能

    C# winform项目中ListView控件使用CheckBoxes属性实现单选功能 在做项目时需要使用ListView控件的CheckBoxes属性显示,还要在点击行时自动选中CheckBoxes ...

  7. ASP.NET js控制treeview中的checkbox实现单选功能

    ASP.NET js控制treeview中的checkbox实现单选功能 function OnTreeNodeChecked() { var element = window.event.srcEl ...

  8. 提高 iOS App 通知功能启用率的三个策略

    我们都知道推送通知在 App 运营中的作用巨大.但是,很多用户却并不买帐,App 第一次启动提示是否「启用推送通知」时,他们直接选择了「否」. 是的,最近我本人就转变成了这样的人 - 认真地评估每个应 ...

  9. iView的tree组件实现单选功能

    iView中的树组件有复选框可以多选,但是目前还没有提供单选框的模式,不显示复选框可以提供高亮单选的模式,但是再次点击就被取消了,没有实现真正的单选: tree 的属性配置中 multiple 是否支 ...

随机推荐

  1. 百练2755:神奇的口袋(简单dp)

    描述有一个神奇的口袋,总的容积是40,用这个口袋可以变出一些物品,这些物品的总体积必须是40.John现在有n个想要得到的物品,每个物品的体积分别是a1,a2……an.John可以从这些物品中选择一些 ...

  2. 【Codeforces 375B】Maximum Submatrix 2

    [链接] 我是链接,点我呀:) [题意] 如果任意行之间可以重新排序. 问你最大的全是1的子矩阵中1的个数 [题解] 设cnt[i][j] 表示(i,j)这个点往右连续的1的个数 我们枚举列j 然后对 ...

  3. [luoguP1026] 统计单词个数(DP)

    传送门 题解 #include <cstdio> #include <cstring> #define max(x, y) ((x) > (y) ? (x) : (y)) ...

  4. noip模拟赛 圆桌游戏

    [问题描述] 有一种圆桌游戏是这样进行的:n个人围着圆桌坐成一圈,按顺时针顺序依次标号为1号至n号.对1<i<n的i来说,i号的左边是i+1号,右边是i-1号.1号的右边是n号,n号的左边 ...

  5. nodejs 安装express

    在命令行中执行 "npm install -g express"等待下载并且自动完成安装.测试express完成安装的一个方法就是查看其版本号,执行命令 “express -V”正 ...

  6. 又通过一道题目,替换字符串 —— 剑指Offer

    https://www.nowcoder.net/practice/4060ac7e3e404ad1a894ef3e17650423?tpId=13&tqId=11155&tPage= ...

  7. 四:Java之字符串操作String、StringBuffer和StringBuilder

    string是我们经经常使用到的一个类型,事实上有时候认为敲代码就是在重复的操作字符串,这是C的特点,在java中.jdk非常好的封装了关于字符串的操作.三个类String .StringBuffer ...

  8. 暴力解hdu4930Fighting the Landlords

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> us ...

  9. tomcat连接mysql的3个问题解决

    转载请标明出处: 本文出自:[ouyida3的博客] 1.BasicDataSourceFactory Caused by: java.lang.ClassNotFoundException: org ...

  10. js 数字格式化,只能输入正负整数,小数

    1.只能输入正整数 <input name="columnValue" class="input96 required" type="text& ...