UITableView 单元格重叠

2024-01-10

我正在实现一个带有部分索引的 tableView。每当任何特定部分中的行超过一行时,我的 tableViewCell 就会重叠。

这是我的代码。

![- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return \[self.keys count\];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    return \[\[\[UILocalizedIndexedCollation currentCollation\] sectionTitles\] objectAtIndex:section\];
    return \[keys objectAtIndex:section\];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
//    return \[\[UILocalizedIndexedCollation currentCollation\] sectionIndexTitles\];
    return keys;

}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return \[\[UILocalizedIndexedCollation currentCollation\] sectionForSectionIndexTitleAtIndex:index\];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = \[tableView dequeueReusableCellWithIdentifier:nil\];
    if (cell == nil) {
        cell = \[\[UITableViewCell alloc\] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier\];

    }
    \[self.exhibitorTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)\];

    UIView* bgview = \[\[UIView alloc\] initWithFrame:CGRectMake(0, 0, 1, 1)\];
    bgview.opaque = YES;
    bgview.backgroundColor = \[UIColor colorWithRed:244.0f/255 green:245.0f/255 blue:246.0f/255 alpha:1.0\];
    \[cell setBackgroundView:bgview\];
    cell.textLabel.font = \[UIFont fontWithName:@"Roboto-Light" size:17\];
    cell.detailTextLabel.font = \[UIFont fontWithName:@"Roboto-Light" size:13\];
    cell.textLabel.backgroundColor = \[UIColor clearColor\];
    cell.detailTextLabel.backgroundColor = \[UIColor clearColor\];
    cell.textLabel.textColor = \[UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0\];

    UIImageView *defaultImage = \[\[UIImageView alloc\]init\];
    \[defaultImage setFrame:CGRectMake(5.0f, 5.0f, 40.0f, 40.0f)\];
    \[cell addSubview:defaultImage\];

    for (int i=0; i<\[self.exhibitorArray count\]; i++) {
        NSString * string = \[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"name"\];
        NSString *firstLetter = \[string substringToIndex:1\];
    if (\[\[keys objectAtIndex:indexPath.section\] isEqualToString:firstLetter\] ) {

    NSString *urlString = \[\[NSString alloc\]init\];
    urlString = @"http://";
    urlString = \[urlString stringByAppendingString:\[NSString stringWithFormat:@"%@",\[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"image"\]\]\];
    NSLog(@"%@",urlString);

    AsyncImageView *asyncImageView = \[\[AsyncImageView alloc\] initWithFrame:CGRectMake(0.0f, 0.0f, 40.0f, 40.0f)\];
    \[asyncImageView setBackgroundColor:\[UIColor clearColor\]\];
    NSString *tmp_string = \[\[NSString alloc\]init\];
    tmp_string = urlString;
    \[asyncImageView loadImageFromURL:\[NSURL URLWithString:tmp_string\]\];
    \[defaultImage addSubview:asyncImageView\];
    defaultImage.contentMode = UIViewContentModeScaleAspectFit;
    tmp_string = nil;
    asyncImageView = nil;
    defaultImage = nil;

    NSString *name_string = \[\[NSString alloc\]init\];
    name_string = \[\[self.exhibitorArray objectAtIndex:i\]valueForKey:@"name"\];
    NSLog(@"%@",name_string);
    UILabel *user_name = \[\[ UILabel alloc\]init \];
    \[user_name setFrame:(CGRectMake(58, 5, 270, 25))\];
    \[user_name setBackgroundColor: \[UIColor clearColor\]\];
    \[user_name setText:name_string\];
    \[user_name setFont:\[UIFont fontWithName:@"Roboto-Light" size:14\]\];
    \[user_name setTextAlignment:NSTextAlignmentLeft\];
    \[user_name setTextColor:\[UIColor colorWithRed:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0\]\];
    \[cell addSubview:user_name\];
    user_name = nil;
        }
    }

    \[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator\];

    return cell;
}

它们不重叠。每次使用单元格时,您都会添加一个附加标签。

您只需向单元格添加一个标签,然后重复使用该标签,而不是每次添加一个新标签。

图像视图等也是如此......

最好的解决方案是使用自定义 UITableViewCell 子类......http://www.appcoda.com/customize-table-view-cells-for-uitableview/ http://www.appcoda.com/customize-table-view-cells-for-uitableview/

可能的解决方案

注意:我不喜欢这个,因为使用了 tag 属性,但它不需要子类化 UITableviewCell 就可以完成它......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

    UIImageView *defaultImageView;
    UILabel *customLabel;

    if (cell == nil) {
        // create the cell and empty views ready to take the content.
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        defaultImageView = //create the image view
        defaultImageView.tag = 3;
        [cell.contentView addSubview:defaultImageView];

        customLabel = //create custom label
        customLabel.tag = 4;
        [cell.contentView addSubview:customLabel];
    } else {
        // get the views that have already been created
        defaultImageView = [cell.contentView viewWithTag:3];
        customLabel = [cell.contentView viewWithTag:4];
    }

    // now populate the views...

    defaultImageView.image = someImage;
    customLabel.text = @"Hello, world";

    return cell;
}

这样,您只需在单元格中创建一个标签和一个图像视图,然后再重复使用它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UITableView 单元格重叠 的相关文章

  • 如何在 Alamofire 中使用“responseDecodable”方法?

    I have been trying to use responseDecodable method from Alamofire but I m getting Generic parameter T could not be infer
  • 为什么我需要 2 个或更多核心数据模型?

    我很想知道谁使用多个核心数据模型以及为什么 有什么好处 我正在开发一个应用程序 我认为我可以从多个模型中受益 但我不确定其他好处 我即将推出的应用程序将适用于 iPad 和 iPhone 的另一个版本 iPad 有 3 个主要内容 iPho
  • iOS8 自签名证书已安装但仍不受信任

    由于我无法控制的原因 我需要使用自签名证书针对平台进行 iOS 开发 它是一个在 SAN 中具有特定 IP 地址的根证书 当证书安装在 OSX 系统帐户下时 所有浏览器现在将正常信任对给定 IP 地址的任何访问 通过电子邮件将同一证书发送到
  • 如何在 iOS 中创建多行表格单元格?

    如何让第二个单元格扩展以适合文本而不是缩放文本 iOS 中有内置的方法可以做到这一点 还是我必须想出一些自制的解决方案 如果您查看 iOS 联系人应用程序 会发现有一个类似地址的框 但我找不到如何实现这一点 对于任何希望将来实现这一目标的人
  • 强制本地化图像或图像资产

    正如在这个问题中 如何强制 NSLocalizedString 使用特定语言 https stackoverflow com questions 1669645 how to force nslocalizedstring to use a
  • UICollectionViewFlowLayout 使其布局无效后重绘单元格

    当您使布局无效时UICollectionViewFlowLayout它为每个单元格创建了一堆新的布局属性 然而 它不会告诉您的单元格重新绘制 这会导致任何图层绘图变形 我不想告诉我的集合重新加载其数据 因为这会删除流属性之间的任何良好转换
  • Objective-C 属性和内存管理

    给出以下属性定义 property nonatomic retain MyObject foo 以下代码是否会导致内存泄漏 self foo MyObject alloc init 看起来 alloc 调用将对象上的保留计数增加到 1 然后
  • 是否可以恢复我删除的 xcdatamodel 文件?

    我愚蠢地删除了它 我删除了其中包含不同版本的文件 我可以恢复到旧快照 但从那时起我就已经完成了工作 是否可以恢复到我的存档之一 或者我只能从那里提交 共享 我仍然可以运行该应用程序 但我需要对数据库进行更改 Xcode 将 xcdatamo
  • ios GPUImage,小尺寸图像处理效果不好?

    我正在尝试为 OCR 准备图像 我使用 GPUImage 来完成 代码工作正常 直到我裁剪图像 裁剪后我得到了糟糕的结果 作物面积 https www dropbox com s e3mlp25sl6m55yk IMG 0709 PNG h
  • 如何在禁用状态下更改 UIButton 图像 alpha?

    我有一个带有图像的 UIButton 在其禁用状态下 该图像应具有 0 3 alpha UIButton button UIButton buttonWithType UIButtonTypeCustom UIImage arrowImag
  • 我的 iPhone 6 获取 iPhone 5 媒体查询

    我不明白这里发生了什么事 我在 CSS 媒体查询中专门针对 iphone 5 media only screen and min device width 320px and max device width 568px some div
  • Xcode 在我的 iPhone 应用程序上运行 codesign 后如何运行脚本?

    我编写了一个脚本 它执行一些步骤来将我的构建打包为临时发行版 我的第一个猜测是在我的 Ad Hoc 目标中使用运行脚本阶段 但看起来协同设计被固定在构建 iPhone 应用程序链的最后位置 有人知道如何在构建过程的最后一刻运行我的脚本 Up
  • AST 文件格式错误或损坏

    我有一个问题 我不知道为什么会发生这种情况 但很可能是因为我错误地按了 移动到垃圾箱 到某些系统框架 我收到一条错误消息 AST 文件格式错误或损坏 找不到 AST 文件引用的文件 Users username myProject Quar
  • Swift 字典映射 - 闭包中的 init

    我有 Swift 字典 private var params String AnyObject 这包含查询项目 例如 lat 40 lon 100 我想将这本词典映射到NSURLQueryItem大批 我想让它 迅速 params map
  • 在 Pickerview 中创建复选标记[重复]

    这个问题在这里已经有答案了 我正在开发一个应用程序 我想在用户选定的行上创建复选标记 For Ex 在UITableView中有 UITableViewCellAccessoryCheckmark 但我想知道是否有类似的东西可用于 UIPi
  • 在 Swift 中计算两个 CLLocation 点之间的方位角 [重复]

    这个问题在这里已经有答案了 我正在尝试计算仅 swift 代码中两个 CLLocation 点之间的方位 我遇到了一些困难 并假设这是一个非常简单的函数 堆栈溢出似乎没有列出任何内容 func d2r degrees Double gt D
  • -[_SwiftValueencodeWithCoder:]:无法识别的选择器发送到实例

    尝试使用 NSCoder 时出现错误 玩家 swift class Player NSObject NSCoding private var playerName String private var playerScore Int pri
  • 如何顺序访问字典?

    我想以元素存储在字典中的方式访问字典 有人可以帮我做这件事吗 提前致谢 字典将其值存储在由键或更精确地由键的哈希值索引的结构中 这就是他们速度很快的原因 他们不需要搜索值 他们只需获取键的值并直接查找值 在大多数情况下 只有在发生冲突的键哈
  • iOS 上的推送通知渐进式 Web 应用程序

    我需要开发一个集成了推送通知的渐进式网络应用程序 在网上搜索我发现了关于这个主题的不同意见 如果我理解正确的话 目前我们无法在移动版 safari 中推送通知 但仅限桌面版 这样对吗 你有什么建议来获得相同的结果吗 我不是iOS专家 我想知
  • 初始化Object中的空字符串?

    有人使用以下方法来初始化 NSstring NSString astring NSString alloc init 我想知道为什么不直接使用 NSString atring nil or NSString astring 没有semant

随机推荐

  • 使用 prerender-spa-plugin 时如何加载 Vuetify?

    当我们在开发模式下运行 Vuetify 应用程序时 使用npm run dev 工作正常 然而 当我们使用prerender spa plugin Vuetify CSS 文件正确加载 但所有 JavaScript 组件都不起作用 即单击按
  • 如何在编辑模式下在 UITableView 中添加额外的单元格?

    您知道在表格进入编辑模式后如何让某些单元格出现在表格视图中吗 就像您编辑联系人时 通讯录 iPhone 应用程序所做的那样 也许我错了 但是在编辑联系人时 看起来像是使用了分组的 UITableView 我试过这个 self tableVi
  • Hg 存储库中的 git 子模块?

    我有一个非常旧的项目 其中直接包含另一个项目的源代码 而不是将其链接为库 回到糟糕的日子 当我将所有内容都保存在 CVS 中时 我将外部代码放在供应商分支上并定期导入 现在我的项目位于 git 中 将外部项目作为子模块包含进来会更有意义 但
  • 如何使用日期选择器在 Angular Material 5.0.0 中选择日期范围?

    我正在使用最新的Angular Material 5 0 0 rc0在我的 Angular 5 应用程序中 我正在尝试选择一个日期范围datepicker提供了 Angular 材料 但我找不到任何相关文档 我所能做的就是选择一个start
  • 最大化 div 的高度,周围元素的高度可变

    我有一个 div 我想在基于 100vh 的父级中最大化其大小 问题是我有两个pdiv 还可以根据窗口的宽度更改其高度 从而导致大小变化 现在 快速但肮脏的解决方案可能只是运行 jQuery 片段来检测父级的大小div and pdiv 并
  • 生成具有 2 个 OU 名称的 CSR

    我必须创建一个生成 CSR 的应用程序 在生成 CSR 时 我们需要填写一些详细信息 例如 CN OU 等 问题是我必须向其发送 CSR 的认证机构需要 2 个 OU 组织单位 名称 我用 google 搜索了很多 但找不到任何使用 ope
  • 如何在 Google Analytics 中跟踪“打开新标签”流量

    我有一个推荐网站 它使用一个网址来访问我的实施了谷歌分析的网站 当用户单击链接时 推荐网站会在同一窗口的新选项卡中打开我的网站 我想为每个推荐网站创建一个配置文件 以便每个配置文件都有自己的有关用户活动和交易转化的报告 我是谷歌分析的新手
  • R - 将 data.frame 转换为多维矩阵

    来自 data frame 的示例 x data frame c 1 1 2 2 3 3 c 1 2 1 2 1 2 c 1 1 1 2 2 2 c 12 14 22 24 34 28 colnames x c Store Dept Yea
  • 将 Google Analytics 添加到 Rails 4.2 应用程序

    我有一个使用 Heroku 部署的 Rails 4 2 应用程序 并且尝试向其中添加 Google Analytics 但是 Google Analytics 没有获取任何会话 有什么建议为什么以及如何解决这个问题吗 CODE app la
  • 在办公室 LAN 上设置 xampp 服务器

    我希望在小型办公室内使用 xampp 设置服务器 我当然会保护 xampp 但为了使我的 web 应用程序可供网络上的其他 4 台电脑使用 我是否只创建一个虚拟主机 有没有办法确保只能在局域网上访问Web应用程序 当前设置包括 4 台计算机
  • OpenGL 旋转

    我正在尝试在 OpenGL 中进行简单的旋转 但一定没有抓住重点 我并不是在寻找具体的修复方法 而是在寻找快速解释或更普遍地解释 OpenGL 旋转的链接 目前我有这样的代码 glPushMatrix glRotatef 90 0 0 0
  • 使用 Hadoop MapRed 排序

    Well 我想知道如何在reduce 任务之后更改简单WordCount 程序的排序顺序 我已经制作了另一个按值排序而不是按键排序的映射 但它仍然按升序排序 有没有一种简单的方法可以做到这一点 更改排序顺序 谢谢 韦洛佐 如果您使用的是旧版
  • Android Work Manager - Work Manager 是否能 100% 确保后台执行完成?

    根据我从文档中读到的内容https developer android com topic libraries architecture workmanager https developer android com topic libra
  • 如何限制pyqt中QLineEdit中的用户输入

    我有一个QLineEdit我想限制QLineEdit仅接受整数 它应该像输入掩码一样工作 但我不想用inputmask 因为如果用户点击QLineEdit光标将位于鼠标单击的位置 用户需要导航到 0 位置并输入他想要的内容 有没有什么替代方
  • 如何在 SQL Server 2012 中设置每周自动备份?

    请建议我如何在 SQL Server 2012 中设置自动数据库备份 我需要对 SQL Server 中的所有数据库 目前仅包含 3 个 进行每周自动备份 该备份在每周五 0100 点 凌晨 1 点 运行 这些备份文件 bak 应放置在 E
  • Swift NSScriptCommand 执行默认实现

    我在 Objective C 中使用了这段代码 implementation KDOrderInfo id performDefaultImplementation NSString theRequest self directParame
  • 使用 MailKit 和 Gmail OAuth 发送邮件

    我正在尝试创建一个应用程序 在客户购买时向他们发送电子邮件 我们有自己的 GMail 帐户 我将用它来发送电子邮件 我已经设置了我的应用程序并在 Google API 控制台中创建了凭据 我发现这个问题 https github com j
  • 如何调用API(Oauth 1.0)?

    我试图调用这个API Oauth1 0标准 https appcenter intuit com api v1 Connection Reconnect https appcenter intuit com api v1 Connectio
  • C/C++宏嵌套替换逻辑

    我正在尝试实现 C C 兼容的宏处理 我可以正确处理许多极端情况 包括此处讨论的情况 了解宏间接扩展自身时 C 预处理器的行为 https stackoverflow com questions 66593868 understanding
  • UITableView 单元格重叠

    我正在实现一个带有部分索引的 tableView 每当任何特定部分中的行超过一行时 我的 tableViewCell 就会重叠 这是我的代码 NSInteger numberOfSectionsInTableView UITableView