ios 基础学习之零基础代码控制实现3

article/2025/8/21 14:33:22

这一节学习一下ios中的tableview控件的简单使用 

UiTableView使用必须实现连个协议 <UITableViewDataSource,UITableViewDelegate>来进行对tableview进行数据管理

1)创建一个新的项目learn3

2)添加一个定义模块view FragmentTableView 并实现连个接口协议 UITableViewDataSource,UITableViewDelegate 并定义一个局部的TableView变量

如FragmentTableView.h代码所示

#import <UIKit/UIKit.h>@interface FragmentTableView : UIView<UITableViewDataSource,UITableViewDelegate>{UITableView *mUiTableView;NSArray *datalist;
}
@end


3) 在 FragmentTableView.m 中对tableview进行管理

1.在 initWithFrame 中进行对  FragmentTableView.h 中声明好的tableview变量进行初始化

如.m中代码:

#import "FragmentTableView.h"@implementation FragmentTableView- (id)initWithFrame:(CGRect)frame
{self = [super initWithFrame:frame];if (self) {// Initialization code// 初始化局部变量mUiTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];[mUiTableView setDataSource:self]; //设置数据源为本身[mUiTableView setDelegate:self]; // 设置协议为本身[self addSubview:mUiTableView]; // 添加tableview控件[mUiTableView release];// 初始化一些测试数据datalist = [[NSArray alloc]initWithObjects:@"上海",@"北京",@"广州",@"深圳",@"香港",@"澳门",@"杭州",@"西湖",@"重庆",@"南京", nil];}return self;
}-(void)dealloc{[datalist release];[super dealloc];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{// Drawing code
}
*/@end



2.接下来我们来实现必要的接口 

(1)在没有看api的 我其实也不知道接下来怎么做了 

所以还是了解一下UITableview :command+鼠标点击去可以看到他也有实现的 

搜 @protocol (command+f)可以看到也定义了几个协议 UITableViewDataSource,UITableViewDelegate 

我们关心的 是要做什么 接下来我们就要找 必须实现的 接口协议了继续搜 @required 可以看到下面有连个方法 如下面代码,这样我们就知道该怎么做了 必须实现的

@protocol UITableViewDataSource<NSObject>@required- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
(2)在FragmentTableView.m中实现上面两个方法 并进行设置数据 

实现 UITableViewDataSource 第一个必须实现的方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{// 从本方法的名称定义 可以看到是所有行的number// 返回数组的总数return datalist.count;
}
实现 UITableViewDataSource 第二个必须实现的方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{// 返回 UITableViewCell 对象 根据方法名称意思是 indexPath cell 每行上 就是每行cell的填充// 先初始化一个 UITableViewCell 这次初始化就不和以前一样 规定他的坐标位置了 直接定义他的样式 并设置标识UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"cell"]];NSUInteger row = [indexPath row]; // 初始化一个无符号intNSLog(@"row:%i",row);cell.textLabel.text = [datalist objectAtIndex:row];return cell;
}

ok该实现的我们也都写好了  接下来在 Viewcontroller.m viewDidLoad 方法中添加FragmentUItableview进行显示

FragmentTableView *tableView = [[FragmentTableView alloc]initWithFrame:CGRectMake(0, 0, 320, 300)];[self.view addSubview:tableView];[tableView release]
command+R 进行运行 ok正常显示数据如下图 (这样不能说算完了 因为我们上面的代码还不好 因为我在来回点击并进行拉页面会报异常,接下来我们进行返回cell中的代码优化)



4)接下来对上面代码进行优化并设计的复杂一点

1.在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 接口中进行修改 如下代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    // 返回 UITableViewCell 对象 根据方法名称意思是 indexPath cell 每行上 就是每行cell的填充
//    // 先初始化一个 UITableViewCell 这次初始化就不和以前一样 规定他的坐标位置了 直接定义他的样式 并设置标识
//    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"cell"]];
//    NSUInteger row = [indexPath row]; // 初始化一个无符号int
//    NSLog(@"row:%i",row);
//    cell.textLabel.text = [datalist objectAtIndex:row];
//    return cell;// command+/ 进行注释上面的代码// 接下来进行优化上面的代码// 定义一个标识static NSString *stringIdentifier = @"cell";int row = [indexPath row];// 声明一个UITableViewCell 并接受 参数tableview中的一个标识cellUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:stringIdentifier];// 判断cell 是否为空 如果为空进行初始化 这样的好处是节省系统资源if (cell == nil) {cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:stringIdentifier]autorelease];}// 接下来设置cell显示内容 可以多添加一点控件进去 我们对这个城市列表加上数字编号 并加一些背景等等// 和上面一样设置显示内容cell.textLabel.text = [datalist objectAtIndex:row];[cell.textLabel setTextAlignment:NSTextAlignmentCenter]; // 设置文字居中显示// 设置cell点击背景 这个只提供了 setSelectedBackgroundView方法 所以必须设置图片 我就随便截了个图用用吧 新建 New Group 文件夹命名为 images 鼠标右键添加截好的图片UIImageView *selectBgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 75/2)];[selectBgView setImage:[UIImage imageNamed:@"bg_item_red.png"]];[cell setSelectedBackgroundView:selectBgView];[selectBgView release];// 添加地区编号 背景图片UIImageView *idNumBgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, (75-45)/4, 45/2, 45/2)];[idNumBgView setImage:[UIImage imageNamed:@"bg_item_circle.png"]];[cell addSubview:idNumBgView];[idNumBgView release];// 添加编号UILabel *idNumLable = [[UILabel alloc]initWithFrame:CGRectMake(20, (75-45)/4, 45/2, 45/2)];[idNumLable setText:[NSString stringWithFormat:@"%i",row]];[idNumLable setTextAlignment:NSTextAlignmentCenter];[idNumLable setFont:[UIFont systemFontOfSize:12]];[idNumLable setBackgroundColor:[UIColor clearColor]];[cell addSubview:idNumLable];return cell;
}
图片资源 


运行效果

2.接下来进行添加点击后获得item下标值 这样更符合我们需求

(1)接下来我们实现 UITableviewDelegate 中的 用户改变item选择方法

// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
实现代码如下:

// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{int row = [indexPath row];if (row != [datalist count]) {UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示"message:[NSString stringWithFormat:@"您选择的是:%@",[datalist objectAtIndex:row]]delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];[alert show];[alert release];}
}

运行结果


5)总结

1.这节专门学习了UITableview的实现具体步骤 及 UITableview item 简单的布局




http://chatgpt.dhexx.cn/article/r2IpR8oD.shtml

相关文章

如何使用IOS自动化测试工具UIAutomation

这篇文章主要介绍了UIAutomation使用实例、应用技巧、基本知识点总结和需要注意事项&#xff0c;具有一定的参考价值 第一部分: 熟悉Instruments的UIAutomation. 第二部分: 控件的获取及操作 第三部分: 自定义自动化脚本 xcode中自带的Instuments工具可以用来进行APP的自动…

iOS之深入解析CocoaPods的GitLab CI与组件自动化构建与发布

一、Gitlab CI/CD 简介 ① GitLab GitLab 是一个利用 Ruby on Rails 开发的开源应用程序&#xff0c;实现一个自托管的 Git 项目仓库&#xff0c;可通过 Web 界面进行访问公开的或者私有的项目。GitLab 拥有与 GitHub 类似的功能&#xff0c;能够浏览源代码&#xff0c;管理缺…

Appium——iOS自动化测试

Appium下载与安装 关于Appium&#xff1a; 详细介绍见Appium官方网站&#xff1a;http://appium.io 安装步骤&#xff1a; 安装node.js&npm安装Appium安装Selenium WebDriver 网上有很多关于Appium安装的教程&#xff0c;这里就不再详述。 http://www.cnblogs.com/enjo…

【软件测试】基于Appium的ios自动化教程

Appium作为一个开源的、跨平台的自动化测试工具&#xff0c;适用于测试原生或混合型移动App&#xff0c;它使用WebDriver协议驱动IOS&#xff0c;Android和Windows应用程序&#xff0c;本篇文章介绍实现ios自动化测试 Appium实现iOS自动化测试 01 启动应用 填写 capability信息…

Appium+Python+Pytest+Allure+Git+Node+Jenkins+Xcode IOS自动化测试从0到1落地

一 . 前言 由于IOS系统比Android系统相对封闭&#xff0c;所以进行IOS自动化测试也是相对复杂&#xff0c;不过大家不用担心&#xff0c;小编已经帮你们踩了大部分坑&#xff0c;网上大部分文章都是CV过来的&#xff0c;没有真正的从0到1落地&#xff0c;我们需要具备开源精神&…

iOS完整学习路线图

今晚特地花时间整理出了iOS的完整学习路线图&#xff0c;希望对大家有帮助

iOS 自动化——技术方案、环境配置

移动端的自动化测试&#xff0c;最常见的是 Android 自动化测试&#xff0c;我个人觉得 Android 的测试优先级会更高&#xff0c;也更开放&#xff0c;更容易测试&#xff1b;而 iOS 相较于 Android 要安全稳定的多&#xff0c;但也是一个必须测试的方向&#xff0c;这个系列文…

Appium + IOS 自动化环境搭建教程(实践+总结+踩坑)

文章目录 前言IOS 自动化相关框架介绍自动化测试类工具内测发布工具Appium驱动IOS测试原理关于 WebDriverAgent 基础环境搭建基础环境 安装内容前提环境通用环境iOS 环境iOS 真机调试环境配置IOS自动化-WebDriverAgent-APPIUM框架原理 利用Appium-Python-Client进行iOS的自动化…

基于Appium的ios自动化教程

Appium作为一个开源的、跨平台的自动化测试工具&#xff0c;适用于测试原生或混合型移动App&#xff0c;它使用WebDriver协议驱动IOS&#xff0c;Android和Windows应用程序&#xff0c;本篇文章介绍实现ios自动化测试 01、Appium实现iOS自动化测试 01、启动应用 填写 capabi…

从0开始的ios自动化测试

最近由于工作内容调整&#xff0c;需要开始弄ios自动化了。网上信息有点杂乱&#xff0c;这边我就按我的实际情况&#xff0c;顺便记录下来&#xff0c;看是否能帮到有需要的人。 环境准备 安装tidevice pip3 install -U “tidevice[openssl]” 它的作用是&#xff0c;帮你绕…

iOS自动化测试之基于模拟器的自动化测试

本文来自霍格沃兹测试开发学社 本文节选自霍格沃兹测试学院内部教材 本章节主要讲解 WebDriverAgent 环境搭建以及如何通过 iOS模拟器完成自动化。 WebDriverAgent简介 WebDriverAgent 是在 iOS 客户端启动了 WebDriver 的 Server&#xff0c;借助这个 server&#xff0c;可以…

【iOS自动化测试】第二章:环境安装

环境依赖 本文采用Win下搭建Appium环境调试iOS设备&#xff0c;Mac下结合jenkins运行出报告 Appium Desktop&#xff08;提供元素定位&#xff0c;服务等&#xff09; Appium Desktop 下载地址python3&#xff08;代码脚本编写&#xff09;Web DriverAgent Appium根据Facebookw…

iOS代码设计学习

组件设计 组件/模块,可以理解成【独立的业务或者功能单位】。至于这个单位的粒度大小&#xff0c;需要自己把握。当写一个类的时候&#xff0c;应该按照高内聚&#xff0c;低耦合的原则去设计该类&#xff0c;涉及多个类之间交互&#xff0c;运用SOLID原则&#xff0c;或其它已…

IOS 自动化

python airtest poco ios真机 case文件&#xff1a; ExecuteCodeIos.text 废话不多说直接上代码&#xff1a; # # -*- encodingutf8 -*- __author__ "UI自动化 ios 脚本"from airtest.core.api import * from poco.drivers.ios import iosPoco import os# 钉…

IOS自动化测试环境搭建(Python Java)

一、前言 IOS的App自动化测试与Android的一样&#xff0c;也可以用appium来进行。但是IOS自动化依赖苹果的osx系统、Xcode构建等&#xff0c;且封闭的系统需要苹果开发者账号才可以驱动真机。Appium的环境配置有点麻烦&#xff0c;可能大部分时间都在处理各种稀奇古怪的报错&am…

【iOS自动化测试】第三章:框架设计

框架总体 pytestallureappium 采用po分层设计集成到jenkins运行 框架结构 名称作用备注BasePage存放相应的封装方法基类层Pages页面层用于元素的定位封装页面层TestCases编写测试用例用例层Report存放报告报告层Public公共的调用方法公共层Log日志存放位置日志层Image用于存放…

iOS自动化测试的那些干货

前言 如果有测试大佬发现内容不对&#xff0c;欢迎指正&#xff0c;我会及时修改。 大多数的iOS App(没有持续集成)迭代流程是这样的 也就是说&#xff0c;测试是发布之前的最后一道关卡。如果bug不能在测试中发现&#xff0c;那么bug 就会抵达用户&#xff0c;所以测试的完整…

iOS自动化,知识点1

知识点1&#xff1a; 初始化连接手机 1.查找udid和包名 2&#xff0c;appium连接ios手机的条件 from appium import webdriver from selenium.webdriver.common.by import By import timedef test_01():desired_caps dict()desired_caps["platformName"]"iOS&…

2023主流技术 Appium+IOS 自动化测试环境搭建

关于Appium for IOS Appium是目前最主流的app自动化测试技术&#xff0c;能够同时支持IOS、Android平台上的移动应用自动化测试&#xff0c;能支持众多语言&#xff0c;包括&#xff1a;Java、Python、Ruby、JavaScript等&#xff1b;并且Appium是开源免费使用的。 目前网上的…

ios自动化-1阶段

使用pytestwda执行自动化 整体结构代码层级 #目前待改进的地方 1.执行速度慢&#xff0c;wda获取dom树的效率太低 2.稳定性差&#xff0c;经常会出现一些弹窗导致失败&#xff0c;可参考安卓弹窗处理 watcher() 优化 3.有部分元素定位不到&#xff0c;目前怀疑是层级问题&…