UITableViewCell 选中时改变高度

2024-01-07 01:58

本文主要是介绍UITableViewCell 选中时改变高度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

从老外那看来的 

原帖是这样的

http://locassa.com/animate-uitableview-cell-height-change/


Animate UITableView Cell Height Change

Animate UITableView Cell Height Change
NERD ALERT! This is a tech article, the faint of heart should browse on…
WE ARE HIRING! Want to work for us? Head to our careers page for more information…  Locassa Careers

Overview

Well this is going to be a fairly short article, but I just wanted to share with you a snippet of magic I discovered quite some time ago. It is something that I posted on StackOverflow and to date is the most up-votes I have received on there, with votes coming in pretty much every day.

The magic is in how you can animate the height change for a UITableViewCell in a normal UITableView.

The source code for this article can be downloaded  here.

Simplicity Defined

This really is as simple as it gets. First you need to implement all of the standard methods that you are required to for a UITableView datasource and delegate. But the key one to look at is the following…

1
2
3
4
5
6
7
8
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:( NSIndexPath  *)indexPath {
     // If our cell is selected, return double height
     if ([ self  cellIsSelected:indexPath]) {
         return  kCellHeight * 2.0;
     }
     // Cell isn't selected so return single height
     return  kCellHeight;
}

What this does is check whether the cell is currently selected and if so returns double the normal height. kCellHeight is a compiler define and cellIsSelected: is a method which takes an indexPath and returns whether the cell is selected or not. This doesn’t come from the cell itself, instead I am just storing the selected state in an NSMutableDictionary keyed on the indexPath.

Animate The Change

So we know that returning a variable height in the heightForRowAtIndexPath method will result in the table sizing the cells accordingly, but how do we animate the change… well simplicity itself… you simply use these two lines…

1
2
[myTableView beginUpdates];
[myTableView endUpdates];

What this results in is the UITableView re-evaluating it’s visible cells and setting their size accordingly but without reloading the data, and the best part? IT ANIMATES IT!

In the demo project I toggle the selected status of a cell and call these magic two lines in the following method…

- ( void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath  *)indexPath;

The Full Code

The full code for the selection method is as follows…

1
2
3
4
5
6
7
8
9
10
11
12
- ( void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath  *)indexPath {
     // Deselect cell
     [tableView deselectRowAtIndexPath:indexPath animated:TRUE];
     // Toggle 'selected' state
     BOOL  isSelected = ![ self  cellIsSelected:indexPath];
     // Store cell 'selected' state keyed on indexPath
     NSNumber  *selectedIndex = [ NSNumber  numberWithBool:isSelected];
     [selectedIndexes setObject:selectedIndex forKey:indexPath];
     // This is where magic happens...
     [demoTableView beginUpdates];
     [demoTableView endUpdates];
}

So download the demo project and see it in action…. marvellous!


这篇关于UITableViewCell 选中时改变高度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/578355

相关文章

Weex入门教程之4,获取当前全局环境变量和配置信息(屏幕高度、宽度等)

$getConfig() 获取当前全局环境变量和配置信息。 Returns: config (object): 配置对象;bundleUrl (string): bundle 的 url;debug (boolean): 是否是调试模式;env (object): 环境对象; weexVersion (string): Weex sdk 版本;appName (string): 应用名字;

react笔记 8-18 事件 方法 定义方法 获取/改变数据 传值

1、定义方法并绑定 class News extends React.Component {constructor(props) {super(props)this.state = {msg:'home组件'}}run(){alert("我是一个run") //方法写在类中}render() {return (<div><h2>{this.state.msg}</h2><button onCli

240907-Gradio插入Mermaid流程图并自适应浏览器高度

A. 最终效果 B. 示例代码 import gradio as grmermaid_code = """<iframe srcdoc='<!DOCTYPE html><html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width" /><title>My static Spa

【前端】animation动画以及利用vue制作简单的透明度改变动画,包含vue生命周期实现

一. 问题描述 想做一个文字透明度从1到0然后再从0到1的css动画。 二. 代码写法 2.1 animation写法 2.1.1 animation属性key 2.1.2 代码展示 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=de

学习整理使用jquery实现获取相同name被选中的多选框值的方法

学习整理使用jquery实现获取相同name被选中的多选框值的方法 <html><head><meta charset="gbk"><!-- 引入JQuery --><script src="https://www.qipa250.com/jquery/dist/jquery.min.js" type="text/javascript"></script></head><body>

HTML5如何成为改变移动互联网幕后的推手

在未来的某一天,我们打开手机,不再需要访问手机应用商店,不论是 Apple的还是Google的,我们只需要点击手机主菜单页面上的一个链接,手机就会立即在它的浏览器上启动一个 “应用程序”;再也不需要flash插件,就能欣赏华丽丽视频画面。   AD:2013云计算架构师峰会课程资料下载   2012年,说HTML5集千宠万爱于一身也毫不夸张,IE、Chrome、Firefox和Opera等

兔子--计算listview的高度,解决listview与scrollview控件冲突

/** * 计算ListView的高度 * * @param listView */ public void setListViewHeightBasedOnChildren(ListView listView) { // 获取ListView对应的Adapter OrderGoodsAdapter listAdapter = (OrderGoodsAdapter) listView.getAda

心觉:如何用一颗心改变世界和自己

关于如何开发自己的潜意识开发的底层逻辑和注意事项 我这里就不赘述了,可以看我前面的文章 今天主要讲一下,如何让我们祈祷的作用发挥到极致 这里提到了祈祷,其实就是正念 把自己的梦想,目标,愿望通过积极的语言 或者生动形象的画面告诉自己的潜意识 你的这些梦想,目标,愿望就会慢慢地变成现实 这是真的,有无数的案例可以证实 我这里就不举例子了,大家可以网上搜 我只讲一下,为什么有些人

js 控制 checkbox不选中

js 控制 checkbox不选中 $("input[name=‘myName’]").prop("checked",false);