首页 / IOS / iOS-自定义cell的方法步骤
iOS-自定义cell的方法步骤
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了iOS-自定义cell的方法步骤,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3892字,纯文字阅读大概需要6分钟。
内容图文
![iOS-自定义cell的方法步骤](/upload/InfoBanner/zyjiaocheng/1285/64664cab01134236a08ecb9cb446aa3f.jpg)
#import "ViewController.h"
#import "MyTableViewCell.h"
#import "AddData_ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
NSMutableArray *list;
UITableView *myTabelView;
NSString *path;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(edit)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(add)];
[self creatTableView];
}
- (void)edit
{
[myTabelView setEditing:!myTabelView.isEditing animated:YES];
}
- (void)add
{
AddData_ViewController *addVc = [[AddData_ViewController alloc]init];
[self.navigationController pushViewController:addVc animated:YES];
}
//编辑tableView的cell
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//盘点编辑中的样式是否是删除的样式
if (editingStyle == UITableViewCellEditingStyleDelete) {
//1.删除数据
//2.更新视图
[list removeObjectAtIndex:indexPath.row];
//把移除后的数据同步到plist里面
BOOL success = [list writeToFile:path atomically:YES];
//如果数据移除成功就删除cell
if (success) {
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}
}
}
//设置是否可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 如果移动的不是同一个位置
if (sourceIndexPath != destinationIndexPath) {
// 1.先保存需要移动的数据;sourceIndexPath.row需要移动的数据
NSDictionary *info = list[sourceIndexPath.row];
// 2.移除需要移动的数据
[list removeObjectAtIndex:sourceIndexPath.row];
// 3.插入数据 destinationIndexPath 可以得到需要移动的位置
[list insertObject:info atIndex:destinationIndexPath.row];
// 同步数据到plist文件
[list writeToFile:path atomically:YES];
}
}
- (void)loadData
{
path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"Detail_Data_ List.plist"];
list = [[NSArray arrayWithContentsOfFile:path] mutableCopy];
NSLog( @"%@",list);
if (myTabelView) {
// reloadData必须在主线程 刷新UI
[myTabelView reloadData];
}
};
- (void)viewWillAppear:(BOOL)animated
{
[self loadData];
}
- (void)creatTableView
{
myTabelView = [[UITableView alloc]initWithFrame: self.view.frame style:UITableViewStylePlain];
myTabelView.delegate = self;
myTabelView.dataSource = self;
[self.view addSubview:myTabelView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *vigetableIndentifire = @"cellID";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:vigetableIndentifire];
if (!cell) {
cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:vigetableIndentifire];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.showImage.image = [UIImage imageNamed:list[indexPath.row][@"ImageName"]];
cell.nameLable.text = [NSString stringWithFormat:@"名字:%@ ",list[indexPath.row][@"name"]];
cell.priceLable .text = [NSString stringWithFormat:@"价格: %@元/斤",list[indexPath.row][@"price"]];
cell.contentLable.text = [NSString stringWithFormat:@"介绍:%@",list[indexPath.row][@"content"]];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/jzq_sir/article/details/47355011
内容总结
以上是互联网集市为您收集整理的iOS-自定义cell的方法步骤全部内容,希望文章能够帮你解决iOS-自定义cell的方法步骤所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。