数据的刷新(UITableViewCell实现点击单元格弹窗更改数据)


模型文件

@interface MJHero : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *intro;

+ (instancetype)heroWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end

@implementation MJHero
+ (instancetype)heroWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
@end

控制器文件

@interface MJViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
@property (nonatomic, strong) NSArray *heros;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation MJViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 设置行高(每一行的高度一致)
    self.tableView.rowHeight = 60;
}

- (NSArray *)heros
{
    if (_heros == nil) {
        // 初始化
        // 1.获得plist的全路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
        
        // 2.加载数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中
        NSMutableArray *heroArray = [NSMutableArray array];
        for (NSDictionary *dict in dictArray) {
            // 3.1.创建模型对象
            MJHero *hero = [MJHero heroWithDict:dict];
            
            // 3.2.添加模型对象到数组中
            [heroArray addObject:hero];
        }
        
        // 4.赋值
        _heros = heroArray;
    }
    return _heros;
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection-----");
    return self.heros.count;
}

// 离开屏幕的cell会怎样

/**
 *  每当有一个cell进入视野范围内,就会调用
 */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cellForRowAtIndexPath-----%d", indexPath.row);
    
    // static修饰局部变量:可以保证局部变量只分配一次存储空间(只初始化一次)
    static NSString *ID = @"hero";
    
    // 1.通过一个标识去缓存池中寻找可循环利用的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    // 2.如果没有可循环利用的cell
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    // 3.给cell设置新的数据
    // 取出模型
    MJHero *hero = self.heros[indexPath.row];
    // 设置cell的数据
    cell.textLabel.text = hero.name;
    cell.detailTextLabel.text = hero.intro;
    cell.imageView.image = [UIImage imageNamed:hero.icon];
    
    return cell;
}

#pragma mark - 代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.取得被点击这行对应的模型
    MJHero *hero = self.heros[indexPath.row];
    
    // 弹框
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"数据展示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    // 设置对话框的类型
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    
    // 取得唯一的那个文本框,显示英雄的名称
    [alert textFieldAtIndex:0].text = hero.name;
    
    [alert show];
    
    // 绑定行号到alertView上
    alert.tag = indexPath.row;
}

#pragma mark - alertView的代理方法
/**
 *  点击了alertView上面的按钮就会调用这个方法
 *
 *  @param buttonIndex 按钮的索引,从0开始
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) return;
    
    // 按钮的索引肯定不是0
    
    // 1.取得文本框最后的文字
    NSString *name = [alertView textFieldAtIndex:0].text;
//    int row = alertView.tag;
//    NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
//    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
//    cell.textLabel.text = name;
    
    // 2.修改模型数据
    int row = alertView.tag;
    MJHero *hero = self.heros[row];
    hero.name = name;
    
    // 3.告诉tableView重新加载模型数据
    // reloadData : tableView会向数据源重新请求数据
    // 重新调用数据源的相应方法取得数据
    // 重新调用数据源的tableView:numberOfRowsInSection:获得行数
    // 重新调用数据源的tableView:cellForRowAtIndexPath:得知每一行显示怎样的cell
    // 全部刷新
//    [self.tableView reloadData];
    
    // 局部刷新
    NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
    [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];
}

//- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    // Deselect : 取消选中
//    NSLog(@"取消选中了第%d行", indexPath.row);
//}

@end


链接: http://pan.baidu.com/s/1dDlQHVF 密码: tufb

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 中,要实现点击单元格切换视图的功能,有以下几个步骤: 1. 创建一个 UITableView,并设置其代理和数据源。 2. 在代理方法 `tableView(_:didSelectRowAt:)` 中,监听单元格点击事件。 3. 在点击事件中,获取点击单元格索引,然后根据需要进行视图切换。 下面是一个简单的示例代码: ```swift import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { let tableView = UITableView() override func viewDidLoad() { super.viewDidLoad() // 设置tableView的frame和代理 tableView.frame = view.bounds tableView.delegate = self tableView.dataSource = self // 注册单元格 tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell") // 添加tableView到视图中 view.addSubview(tableView) } // 返回单元格数量 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 5 } // 创建和配置单元格 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = "Cell \(indexPath.row + 1)" return cell } // 单元格点击事件 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // 获取点击单元格索引 let selectedRow = indexPath.row // 根据需要进行视图切换 switch selectedRow { case 0: // 切换到视图1 let viewController1 = ViewController1() navigationController?.pushViewController(viewController1, animated: true) case 1: // 切换到视图2 let viewController2 = ViewController2() navigationController?.pushViewController(viewController2, animated: true) // 其他切换逻辑... default: break } // 取消选中效果 tableView.deselectRow(at: indexPath, animated: true) } } ``` 在上述示例中,我们创建了一个包含 5 个单元格UITableView,并通过点击单元格来切换到不同的视图。你可以根据自己的需求,定义不同的视图,并在 `didSelectRowAt` 方法中进行切换逻辑的处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值