Original URL: http://www.kancloud.cn/digest/ios-1/107420
In the previous section, we defined a single cell, just monotonous input text and insert pictures, but in the actual development, some cells have buttons on it, some cells have sliding controls, some cells have switch options and so on, specifically to participate in the following 2 diagram comparison:
@ We can achieve customization in 2 ways, one is to take advantage of the system's UITableViewCell (but not recommended, because development efficiency is not high), for example: or in this key method
(UITableViewCell)tableView:(UITableView)tableView cellForRowAtIndexPath:(NSIndexPath)indexPath{ static NSString cellIdentifier = @"cell"; UITableViewCell cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentifier]; if(!cell) { cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];
First of all, the initialization of the various button controls must be placed in the IF statement, if placed outside the curly brace, the cell is executed n times, then a cell above will be added n controls
Second, after you customize the initialization control in this parenthesis, if you want to display different content and effects for each cell's control, you can't get the object outside of the parentheses, only by setting them to inherit the UIView property tag to identify, we can think about, If the control is more or others to accept your project, you define a lot of tags, so the efficiency of cooperation is not high, so the main recommendation of the second
} return cell;}
@ Two, create a UITableViewCell subclass, implement a custom effect on contentview (everything on the cell is displayed on the cell's property Contentview), and here's how to write it
#import<UIKit/UIKit.h>@interfaceHmtassistcell:UITableViewCell@property (nonatomic)UILabel * LABEL;@property (nonatomic)UIButton * button;@end-(ID) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) reuseidentifier{Self = [Super Initwithstyle:style Reuseidentifier:reuseidentifier];if (Self) {_button = [UIButton Buttonwithtype:Uibuttontypesystem]; _button. backgroundcolor = [Uicolor Redcolor]; _button. frame =CGRectMake (150,60,50,100); [_button Settitle:@ "Fried fritters" forstate:UIControlStateNormal]; [Self. Contentview Addsubview:_button]; _label = [[UILabel Alloc]initwithframe:CGRectMake (10,30,100,100)]; _label. backgroundcolor = [Uicolor Greencolor]; [Self. Contentview Addsubview:_label]; }ReturnSelf;} - (UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//still set reuse identity static NSString * Cellidentifier = @ "cell"; //here with our new UITableViewCell sub-class for cell reuse declaration Hmtassistcell * cell = (Hmtassistcell *) [TableView Dequeuereusablecellwithidentifier:cellidentifier]; //if not, create if (!cell) {cell = [[Hmtassistcell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellidentifier]; } //because _label is a property in class Hmtassistcell, it is easy to take out for assignment Cell.text = [nsstring stringwithformat:@"%d ", Indexpath.row]; return cell;
"Go" uitableview detailed (UITableViewCell