重复使用带子视图的单元格以编程方式

如何解决重复使用带子视图的单元格以编程方式

| 我在以编程方式将子视图添加到UITableView单元时遇到问题。我试图将UITextFiled添加到2个单元格,并将UILabel添加到表的一个部分中的另一个单元格。该表包含3个部分,其中大部分是手动创建的。选中其中一个单元格会调用一个动作,该动作将再插入四个单元格。如果再次选择相同的单元格,则将删除四个单元格。发生此操作时,子视图会混合在一起并且位于错误的单元格上。键盘也不响应完成键。 我尝试在if(cell == nil)条件语句中创建子视图。这是我创建单元的代码(对不起,它是如此混乱,我一直在尝试很多事情):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @\"Cell\";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSLog(@\"cell created\");
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    if (indexPath.section == 0 && indexPath.row == 0 && ([cellText objectAtIndex:indexPath.row] == @\"Weight\")) {
        exerciseWeight = [[[UITextField alloc] initWithFrame:CGRectMake(0,12,295,30)] autorelease];
        exerciseWeight.textAlignment = UITextAlignmentRight;
        exerciseWeight.adjustsFontSizeToFitWidth = YES;
        exerciseWeight.textColor = [UIColor blackColor];
        exerciseWeight.backgroundColor = [UIColor clearColor];
        exerciseWeight.tag = 89899;
        exerciseWeight.keyboardType = UIKeyboardTypeNumberPad;
        [exerciseWeight setReturnKeyType:UIReturnKeyDone];
        exerciseWeight.clearButtonMode = UITextFieldViewModeNever;
        [exerciseWeight setEnabled: YES];
        [exerciseWeight setDelegate:self];
        [cell addSubview:exerciseWeight];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;

    } else if (indexPath.section == 0 && indexPath.row == 1) {
        NSLog(@\"Created metric subview\");
        metric = [[[UILabel alloc] initWithFrame:CGRectMake(100,5,180,30)] autorelease];
        metric.textAlignment = UITextAlignmentRight;
        metric.textColor = [UIColor grayColor];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        metric.tag = 89898;
        [cell addSubview:metric];
    } else if (([cellText count] - 1) == indexPath.row){
        exerciseReps = [[[UITextField alloc] initWithFrame:CGRectMake(0,30)] autorelease];
        exerciseReps.textAlignment = UITextAlignmentRight;
        exerciseReps.adjustsFontSizeToFitWidth = YES;
        exerciseReps.backgroundColor = [UIColor clearColor];
        exerciseReps.tag = 89890;
        exerciseReps.keyboardType = UIKeyboardTypeNumberPad;
        [exerciseReps setReturnKeyType:UIReturnKeyDone];
        exerciseReps.clearButtonMode = UITextFieldViewModeNever;
        [exerciseReps setEnabled:YES];
        [exerciseReps setDelegate:self];
        [cell addSubview:exerciseReps];
    } else if (indexPath.section == 2) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
} else {
    exerciseWeight = (UITextField *)[cell viewWithTag:89899];
    metric = (UILabel *)[cell viewWithTag:89898];
    exerciseReps = (UITextField *)[cell viewWithTag:89890];
}
if (indexPath.section == 0) {
    cell.textLabel.text = [cellText objectAtIndex:indexPath.row];
}
// Configure the cell...
if (indexPath.section == 0 && indexPath.row == 0) {
    cell.textLabel.textAlignment = UITextAlignmentLeft;
    exerciseWeight.placeholder = @\"160\";


} else if (indexPath.section == 0 && indexPath.row == 1) {
    metric.text = metricUnit;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
} else if (cell.textLabel.text == @\"Repetitions\"){
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryNone;
    exerciseReps.placeholder = @\"10\";
} else if (indexPath.section == 1) {
    cell.textLabel.text = @\"Log New Set\";
    cell.textLabel.textAlignment = UITextAlignmentCenter;
    cell.accessoryType = UITableViewCellAccessoryNone;
} else if (indexPath.section == 2) {
    cell.textLabel.text = @\"History\";

} else if (cell.textLabel.text == @\"  Pounds\") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
} else if (cell.textLabel.text == @\"  Kilograms\") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
} else if (cell.textLabel.text == @\"  Miles\") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
} else if (cell.textLabel.text == @\"  Kilometers\") {
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
}

return cell;

// [exerciseWeight release];
// [metric release];
// [exerciseReps release]; 
}
    

解决方法

        看起来您会在这里玩得开心。但是,一旦完成,它就会令人满意:-) 我不确定您发布的代码是否需要担心。定义您在表中看到的内容(主要是)是诸如heightForRowAtIndexPath,numberOfSectionsInTableView和numberOfRowsInSection之类的方法。如果您做对了,您将得到想要的显示。因此,当您点击一个单元格时,您将修改您的基础数据结构,然后调用reloadData-导致表视图再次重新调用这些方法。 关于布局这些单元格,我根本看不到您使用该单元格的contentView。您是否看过Cocoa With Love博客,特别是Easy自定义UITableView绘图和UITableView的构造,绘图和管理(已重新访问)?我还使用了xib方法(如Apple文档,iOS的“表视图编程指南”中所述)来定义表视图单元格,并且鉴于Xcode一直保持改进接口构建器与代码交互的方式,这种方法可能会继续这样做。我非常反对IB,但是现在看到使用它的价值,因为Xcode 4消除了切换应用程序以使用它的痛苦,出现错误的机会更少了。 我在想的是,当您向某个单元格“添加视图”时,您应该只切换到您定义的另一个单元格,并根据需要在reloadData之后加载该单元格。当您要添加单元格时,您可以更改基础数据结构,并让表格自行显示。 关于单元格标识符的问题-您可以使用一个单元格标识符并仅节省创建单元格的成本,将您对所有单元格所做的所有常见事情都排除在外,或者您可以为每种单元格类型创建具有唯一标识符的单元格,当您的节/行指示需要TK421时,请重用单元变体TK421。我倾向于做第二个。我实际上不知道哪一个性能更好,但是我敢肯定,这两者总比没有重用要好。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-