UITableViewCell如何知道自己的indexPath? [英] How can a UITableViewCell know of its own indexPath?

查看:99
本文介绍了UITableViewCell如何知道自己的indexPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准的分组 UITableView 样式允许在每个顶部和底部用圆角绘制 UITableViewCell s部分。这是如何完成的?细胞如何知道它自己在其部分内的位置,以及它如何知道何时更改其圆形边缘?

The standard Grouped UITableView style allows UITableViewCells to be drawn with rounded corners at the top and bottom of each section. How is this accomplished? How does the cell know its own location within its section, and how does it know when to change its rounded edges?

我想制作自己的圆形细胞,而我有图像可以使用,但不知道何时显示哪个图像

I want to make my own rounded cells, and I have images to use, but don't know when to show which image

注意:我已经知道UITableView是如何工作的,我知道如何使用它。我只是认为,因为UITableView能够在正确的位置自动绘制圆角,所以我也应该能够,无需向我的数据源或委托添加任何内容。

Note: I already know how the UITableView works, and I know how to use it. I just thought that since a UITableView is able to automatically draw rounded corners at the correct places, I should be able to as well, without needing to add anything to my data source or delegate.

推荐答案

sectionLocation UITableViewCell 的方法返回整数你需要什么:

There is sectionLocation method of UITableViewCell that returns integer telling you what you need:


  • 1 - 中间单元格

  • 2 - 顶部单元格

  • 3 - 底部单元格

  • 4 - 单个单元格

  • 1 - middle cell
  • 2 - top cell
  • 3 - bottom cell
  • 4 - single cell

I自2010年以来在几个生产应用程序中使用它没有任何问题。在您的头文件中添加这样的东西,你可以使用它:

I had no issues using this in several production apps since 2010. Add something like this into your header files and you can use it:

typedef NS_ENUM(NSInteger, MMMTableViewCellLocation) {

    MMMTableViewCellLocationUndefined = 0,
    MMMTableViewCellLocationMiddle = 1,
    MMMTableViewCellLocationTop = 2,
    MMMTableViewCellLocationBottom = 3,
    MMMTableViewCellLocationSingle = 4
};

@interface UITableViewCell ()

/** Undocumented method of UITableViewCell which allows to know where within section the cell is located,
 * so the cell can draw its borders properly. */
- (MMMTableViewCellLocation)sectionLocation;

/** Override this one to know when the value of sectionLocation changes. */
- (void)setSectionLocation:(MMMTableViewCellLocation)sectionLocation animated:(BOOL)animated;

@end

这篇关于UITableViewCell如何知道自己的indexPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆