:自定义Cell中的代码

#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:@"choice"];
}
else
{
self.mark_ImageView.hidden = YES;
} } @end :ViewController中的代码 #import "ViewController.h"
#import "TestCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate> @property(nonatomic,strong)NSMutableArray *dataArray; /**
* 保存选中行的对应的数据
*/
@property(nonatomic,strong)NSMutableArray *markArray; /**
* 保存标记选中行字典的数组
*/
@property(nonatomic,strong)NSMutableArray *checkArray; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; for (NSInteger i=; i<; i++) {
[self.dataArray addObject:[NSString stringWithFormat:@"选中第%ld行",i]];
} for (NSInteger i =; i<self.dataArray.count; i++) {
NSMutableDictionary *resultDict = [NSMutableDictionary dictionaryWithObject:@ forKey:@"isChecked"];
[self.markArray addObject:resultDict];
} } - (NSMutableArray *)dataArray{ if (!_dataArray) {
_dataArray =[NSMutableArray array];
}
return _dataArray; } - (NSMutableArray *)markArray{ if (!_markArray) {
_markArray =[NSMutableArray array];
}
return _markArray; } - (NSMutableArray *)checkArray{ if (!_checkArray) {
_checkArray =[NSMutableArray array];
}
return _checkArray; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
}
-(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];
NSDictionary *resultDict = self.markArray[indexPath.row];
cell.isChecked = [resultDict[@"isChecked"] boolValue];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44.0f;
} - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
TestCell *cell = (TestCell *)[tableView cellForRowAtIndexPath:indexPath];
NSString *checkString = self.dataArray[indexPath.row];
NSMutableDictionary *checkedResult = self.markArray[indexPath.row];
//isChecked为NO则表明要把这行置为选中状态
if ([checkedResult[@"isChecked"] boolValue]==NO) {
[checkedResult setValue:@ forKey:@"isChecked"];
cell.isChecked = YES;
[self.checkArray addObject:checkString]; }
else{
[checkedResult setValue:@ forKey:@"isChecked"];
cell.isChecked = NO;
[self.checkArray removeObject:checkString]; }
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//刷新指定的行
NSIndexPath *indexPath_Row=[NSIndexPath indexPathForRow:indexPath.row inSection:];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath_Row,nil] withRowAnimation:UITableViewRowAnimationNone];
NSLog(@"self.checkArray =%@",self.checkArray); } @end

最新文章

  1. DB2 中文排序问题
  2. JavaScript 页面模板引擎
  3. 修改crontab默认的编辑器
  4. 专访Linux嵌入式开发韦东山操作系统图书作者--转
  5. javaScript内置类Date,Math等
  6. 项目SVN的IP地址发生变化时修改SVN为新的IP地址
  7. Recommender Systems 基于知识的推荐
  8. Aggregation Models
  9. W5500 keep-alive的用途及使用
  10. 如何高效撤销Git管理的文件在各种状态下的更改
  11. 利用whoosh对mongoDB的中文文档建立全文检索
  12. TestNG entryset的用法及遍历map的用法
  13. 4. 带有延迟时间的Queue(DelayQueue)
  14. odoo8资料
  15. mac 10.12显示隐藏文件
  16. 教你如何在linux下查看服务是否已经启动或者关闭
  17. (3)什么是函数(函数的定义、形参、实参、默认形参、可变长函数args|kwargs)
  18. jQuery基础(鼠标事件,表单事件,键盘事件,自定义事件 篇)
  19. Qt ------ 截图、获取鼠标指定的RGB值
  20. poj 1328 Radar Installation 排序贪心

热门文章

  1. RequestMapping_请求参数&amp;请求头
  2. 【Codeforces 1096D】Easy Problem
  3. HDU 1224 无环有向最长路
  4. 在代码动态设置RelativeLayout的属性,比如layout_below
  5. Lotto(DFS处理)
  6. QT如何修改字符编码格式
  7. 国际维修联合会IMA年度大会在瑞士圆满结束
  8. ViewPager中View的复用
  9. UIView convertRect
  10. Mongo使用