[课堂实践与项目]系统UITableViewCell的内嵌属性的说明和部分属性使用

1.今天学习了UITableViewCell的有关属性。没有牵扯自定义的Cell。我们就先来看看UITableViewCell的有关属性和使用方法。

1.- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 

 NSString *cellIndentifiter = @"CellIndentifiter";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifiter];


2.如图

@property (nonatomic, readonly, retain) UIImageView *imageView

3.如图

@property (nonatomic, readonly, retain) UILabel     *textLabel NS_AVAILABLE_IOS(3_0); 

4.如图

@property (nonatomic, readonly, retain) UILabel     *detailTextLabel

    NSString *textfont =[self.array objectAtIndex:row];
    cell.textLabel.font = [UIFont fontWithName:textfont size:20];
    cell.textLabel.text = textfont;
    cell.detailTextLabel.text = @"detailText";
    cell.imageView.image = [UIImage imageNamed:@"1.png"];


//------------

5.

// If you want to customize cells by simply adding additional views, you should add them to the content view so they will be positioned appropriately as the cell transitions into and out of editing mode.

@property (nonatomic, readonly, retain) UIView      *contentView;

6.

// Default is nil for cells in UITableViewStylePlain, and non-nil for UITableViewStyleGrouped. The 'backgroundView' will be added as a subview behind all other views.

@property (nonatomic, retain) UIView                *backgroundView;

7.

@property (nonatomic, retain) UIView                *selectedBackgroundView;

8.

// If not nil, takes the place of the selectedBackgroundView when using multiple selection.

@property (nonatomic, retain) UIView                *multipleSelectionBackgroundView NS_AVAILABLE_IOS(5_0);

9.

@property (nonatomic, readonly, copy) NSString      *reuseIdentifier;

- (void)prepareForReuse;                                                        // if the cell is reusable (has a reuse identifier), this is called just before the cell is returned from the table view method dequeueReusableCellWithIdentifier:.  If you override, you MUST call super.

10.

@property (nonatomic) UITableViewCellSelectionStyle   selectionStyle;    

11.

@property (nonatomic, getter=isSelected) BOOL         selected;   

12.

@property (nonatomic, getter=isHighlighted) BOOL      highlighted; 

13。

- (void)setSelected:(BOOL)selected animated:(BOOL)animated;    

14.

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated;   

15.

@property (nonatomic, readonly) UITableViewCellEditingStyle editingStyle;         // default is UITableViewCellEditingStyleNone. This is set by UITableView using the delegate's value for cells who customize their appearance accordingly.

@property (nonatomic) BOOL                            showsReorderControl;        // default is NO

@property (nonatomic) BOOL                            shouldIndentWhileEditing;   // default is YES.  This is unrelated to the indentation level below.

16.

@property (nonatomic) UITableViewCellAccessoryType    accessoryType;              // default is UITableViewCellAccessoryNone. use to set standard type

typedef NS_ENUM(NSInteger, UITableViewCellAccessoryType) {

    UITableViewCellAccessoryNone,                   // don't show any accessory view

    UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track

    UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks

    UITableViewCellAccessoryCheckmark,              // checkmark. doesn't track

    UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // info button. tracks

};







//-----------------

17.

@property (nonatomic, retain) UIView                 *accessoryView;              // if set, use custom view. ignore accessoryType. tracks if enabled can calls accessory action

//accessoryView 是可以设置的,但是 xy的位置是固定不可变的。和accessoryType 所处的位置相同,但是如果改变view的宽高,还是可以改变的



18.

@property (nonatomic) UITableViewCellAccessoryType    editingAccessoryType;       // default is UITableViewCellAccessoryNone. use to set standard type

@property (nonatomic, retain) UIView                 *editingAccessoryView;       // if set, use custom view. ignore editingAccessoryType. tracks if enabled can calls accessory action

19

@property (nonatomic) NSInteger                       indentationLevel;           // adjust content indent. default is 0

@property (nonatomic) CGFloat                         indentationWidth;           // width for each level. default is 10.0

@property (nonatomic) UIEdgeInsets                    separatorInset NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; // allows customization of the separator frame

20

@property(nonatomic, getter=isEditing) BOOL           editing;                    // show appropriate edit controls (+/- & reorder). By default -setEditing: calls setEditing:animated: with NO for animated.

- (void)setEditing:(BOOL)editing animated:(BOOL)animated;

21

@property(nonatomic, readonly) BOOL                   showingDeleteConfirmation;  // currently showing "Delete" button


22

// These methods can be used by subclasses to animate additional changes to the cell when the cell is changing state

// Note that when the cell is swiped, the cell will be transitioned into the UITableViewCellStateShowingDeleteConfirmationMask state,

// but the UITableViewCellStateShowingEditControlMask will not be set.

- (void)willTransitionToState:(UITableViewCellStateMask)state NS_AVAILABLE_IOS(3_0);

- (void)didTransitionToState:(UITableViewCellStateMask)state NS_AVAILABLE_IOS(3_0);

typedef NS_OPTIONS(NSUInteger, UITableViewCellStateMask) {

    UITableViewCellStateDefaultMask                     = 0,

    UITableViewCellStateShowingEditControlMask          = 1 << 0,

    UITableViewCellStateShowingDeleteConfirmationMask   = 1 << 1

};



iOS-RATreeView是一个开源的第三方库,提供了多层级的UITableView展示功能。使用该库可以轻松实现多级列表的展开与收起。 首先,在项目中引入iOS-RATreeView库。可以使用CocoaPods引入,也可以手动下载并导入。 接下来,在需要使用多级列表的UIViewController中,创建一个RADataObject类型的数组,用来存储数据。RADataObject是iOS-RATreeView中的一个数据模型,用来表示一条记录。每个RADataObject可以包含多个子RADataObject,从而形成多级列表。 ``` // 创建RADataObject数组 NSMutableArray *data = [NSMutableArray array]; // 创建一级列表 RADataObject *level1_1 = [RADataObject dataObjectWithName:@"Level 1-1" children:nil]; RADataObject *level1_2 = [RADataObject dataObjectWithName:@"Level 1-2" children:nil]; RADataObject *level1_3 = [RADataObject dataObjectWithName:@"Level 1-3" children:nil]; // 创建二级列表 RADataObject *level2_1 = [RADataObject dataObjectWithName:@"Level 2-1" children:nil]; RADataObject *level2_2 = [RADataObject dataObjectWithName:@"Level 2-2" children:nil]; RADataObject *level2_3 = [RADataObject dataObjectWithName:@"Level 2-3" children:nil]; // 将二级列表添加到一级列表中 level1_1.children = @[level2_1, level2_2]; level1_2.children = @[level2_3]; // 将一级列表添加到RADataObject数组中 [data addObject:level1_1]; [data addObject:level1_2]; [data addObject:level1_3]; ``` 创建完数据源后,需要创建RATreeView对象,并设置代理和数据源。 ``` // 创建RATreeView对象 self.treeView = [[RATreeView alloc] initWithFrame:self.view.bounds]; // 设置代理和数据源 self.treeView.delegate = self; self.treeView.dataSource = self; ``` 接下来实现RATreeViewDataSource协议中的方法,用来返回列表的数据。具体实现可以参考下面的代码。 ``` - (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item { static NSString *identifier = @"Cell"; UITableViewCell *cell = [treeView dequeueReusableCellWithIdentifier:identifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; } // 获取RADataObject对象 RADataObject *dataObject = item; // 设置cell的文本 cell.textLabel.text = dataObject.name; return cell; } - (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item { if (item == nil) { // 如果item为nil,表示请求根节点的子节点数量 return self.data.count; } else { // 获取RADataObject对象 RADataObject *dataObject = item; // 返回子节点数量 return dataObject.children.count; } } - (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item { if (item == nil) { // 如果item为nil,表示请求根节点的子节点 return self.data[index]; } else { // 获取RADataObject对象 RADataObject *dataObject = item; // 返回子节点 return dataObject.children[index]; } } - (BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item { // 返回是否可以编辑 return YES; } - (void)treeView:(RATreeView *)treeView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowForItem:(id)item { if (editingStyle == UITableViewCellEditingStyleDelete) { // 删除节点 RADataObject *parentObject = [treeView parentForItem:item]; if (parentObject) { NSMutableArray *children = [NSMutableArray arrayWithArray:parentObject.children]; [children removeObject:item]; parentObject.children = children; } else { NSMutableArray *data = [NSMutableArray arrayWithArray:self.data]; [data removeObject:item]; self.data = data; } // 刷新列表 [treeView reloadData]; } } ``` 最后,在RATreeViewDelegate协议中实现展开与收起节点的方法。 ``` - (void)treeView:(RATreeView *)treeView willExpandRowForItem:(id)item { // 获取RADataObject对象 RADataObject *dataObject = item; // 设置节点的展开状态 dataObject.expanded = YES; } - (void)treeView:(RATreeView *)treeView willCollapseRowForItem:(id)item { // 获取RADataObject对象 RADataObject *dataObject = item; // 设置节点的展开状态 dataObject.expanded = NO; } ``` 至此,多级列表展开与收起的功能就实现了。在需要展示多级列表的地方,只需要将创建的RATreeView添加到视图中即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值