ios-day06-04(UITableView的全局刷新和局部刷新、UIAlertView的使用)

本文详细介绍了如何在iOS应用中实现数据刷新功能,并通过展示英雄数据实例,展示了如何利用UITableView来动态更新界面内容。同时,文章还探讨了如何通过弹框交互来动态修改模型数据并刷新视图,提供了iOS开发中数据管理和用户交互的有效实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源码下载地址:http://download.youkuaiyun.com/detail/liu537192/8463517


效果图:

          


         


核心代码:

//
//  LiuJieViewController.m
//  04-数据刷新
//
//  Created by XinYou on 15-2-28.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "LiuJieViewController.h"
#import "LiuJieHero.h"

@interface LiuJieViewController () 
   
    
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic,strong)NSArray *heros;

@end

@implementation LiuJieViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// 给UITableView设置数据源
    self.tableView.dataSource = self;
    // 给UITableView设置代理
    self.tableView.delegate = self;
    
    // 设置行高
    self.tableView.rowHeight = 60;
}

/**
 *  隐藏状态栏
 */
- (BOOL)prefersStatusBarHidden{

    return YES;
}

- (NSArray *)heros{

    if (_heros == nil) {
        // 获取plist文件的路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
        
        // 加载数组
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        // 将dictArray中得所有字典对象转成模型对象,放到新的数组中
        NSMutableArray *tempArray = [NSMutableArray array];
        
        for (NSDictionary *dict in dictArray) {
            // 创建模型对象
            LiuJieHero *hero = [LiuJieHero heroWithDict:dict];
            // 添加模型对象到数组中
            [tempArray addObject:hero];
        }
        
        // 赋值
        _heros = tempArray;
    }
    
    return _heros;
}

#pragma mark -数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.heros.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 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设置新的数据
    LiuJieHero *hero = self.heros[indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:hero.icon];
    cell.textLabel.text = hero.name;
    cell.detailTextLabel.text = hero.intro;
    
    return cell;
}

#pragma mark -代理方法
/**
 *  当用户点击了某一行,就会执行这个方法
 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//    NSLog(@"点击了第%d行",indexPath.row);
    
    // 取得被点击这行对应的模型
    LiuJieHero *hero = self.heros[indexPath.row];
    
    // 创建弹框
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"数据展示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    
    // 设置对话框的类型
    // UIAlertViewStyleDefault 默认的样式的对话框
    // UIAlertViewStyleSecureTextInput 带有一个密文输入框的对话框
    // UIAlertViewStylePlainTextInput 带有一个普通输入框的对话框
    // UIAlertViewStyleLoginAndPasswordInput 带有两个输入框(一个普通和一个密文)的对话框
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    
    // 设置对话框中输入框的文本
    [alert textFieldAtIndex:0].text = hero.name;
    
    // 显示弹框
    [alert show];
    
    // 绑定行号到alertView上
    alert.tag = indexPath.row;
}

#pragma mark -alertView的代理方法
/**
 *  点击了alertView上面的按钮就会调用这个方法
 */
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    // 如果按钮的索引为0,表示点击了“取消”按钮
    if (buttonIndex == 0) {
        return;
    }
    
    // 1 取得文本输入框最后的文字
    NSString *name = [alertView textFieldAtIndex:0].text;
    
    // 2 修改模型数据
    int row = alertView.tag;
    LiuJieHero *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];
    // 3 局部刷新
    [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];
}

@end
//
//  LiuJieHero.h
//  04-数据刷新
//
//  Created by XinYou on 15-2-28.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
    
     

@interface LiuJieHero : NSObject
/**
 *  每个英雄对应的图片
 */
@property (nonatomic,copy) NSString *icon;
/**
 *  每个英雄对应的名字
 */
@property (nonatomic,copy) NSString *name;
/**
 *  每个英雄的介绍
 */
@property (nonatomic,copy) NSString *intro;

+ (instancetype)heroWithDict:(NSDictionary *)dict;

- (instancetype)initWithDict:(NSDictionary *)dict;

@end
//
//  LiuJieHero.m
//  04-数据刷新
//
//  Created by XinYou on 15-2-28.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "LiuJieHero.h"

@implementation LiuJieHero

- (instancetype)initWithDict:(NSDictionary *)dict{

    if (self = [super init]) {
        self.icon = dict[@"icon"];
        self.name = dict[@"name"];
        self.intro = dict[@"intro"];
        
        // 也可以使用KVC
//        [self setValuesForKeysWithDictionary:dict];
    }
    
    return self;
}

+ (instancetype)heroWithDict:(NSDictionary *)dict{

    return [[self alloc] initWithDict:dict];
}

@end

    
   

基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。 智能教学辅助系统 这是一个智能教学辅助系统的前端项目,基于 Vue3+TypeScript 开发,使用 Ant Design Vue 作为 UI 组件库。 功能模块 用户模块 登录/注册功能,支持学生教师角色 毛玻璃效果的登录界面 教师模块 备课与设计:根据课程大纲自动设计教学内容 考核内容生成:自动生成多样化考核题目及参考答案 学情数据分析:自动化检测学生答案,提供数据分析 学生模块 在线学习助手:结合教学内容解答问题 实时练习评测助手:生成随练题目并纠错 管理模块 用户管理:管理员/教师/学生等用户基本管理 课件资源管理:按学科列表管理教师备课资源 大屏概览:使用统计、效率指数、学习效果等 技术栈 Vue3 TypeScript Pinia 状态管理 Ant Design Vue 组件库 Axios 请求库 ByteMD 编辑器 ECharts 图表库 Monaco 编辑器 双主题支持(专业科技风/暗黑风) 开发指南 # 安装依赖 npm install # 启动开发服务器 npm run dev # 构建生产版本 npm run build 简介 本项目旨在开发一个基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值