To customize a cell by inheriting UITableViewCell
1, create an empty project, named:
2. Create a Uitableviewcontroller and create xib at the same time:
3. Set the root controller of windows in APPDELEGATE.M for the tableviewcontroller you just created:
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
Tableviewcontroller *tableviewcontroller = [[[Tableviewcontroller alloc] init] autorelease]; Automatic release
//Set root controller
self.window.rootViewController = Tableviewcontroller;
[Self.window makekeyandvisible];
return YES;
4. Create custom UITableViewCell:
5. Create a custom cell xib drag and drop the controls you want
Select User Interface.
Create an empty xib.
Drag into the cell control.
Complete the custom cell control.
Sets the identfier of the cell control.
Binds the cell class and associates the control's exports with the TableViewCell.h file.
6, to the Tableviewcontroller class code, in the delegate method to set the custom cell:
#import "TableViewController.h" #import "TableViewCell.h" @interface Tableviewcontroller () {Nsmutablearray *table Data; Tabular data} @end @implementation Tableviewcontroller-(ID) Initwithstyle: (Uitableviewstyle) style {self = [supe
R Initwithstyle:style];
if (self) {//Custom initialization} return self;
}-(void) viewdidload {[Super viewdidload];
Initialize table Data Tabledata = [[Nsmutablearray alloc] init];
for (int i = 0; i< i++) {[Tabledata addobject:[nsstring stringwithformat:@ ' mycelldemon%i ', I]];
//Set the height of row to the height of the custom cell self.tableView.rowHeight = 90;
}-(void) didreceivememorywarning {[Super didreceivememorywarning]; } #pragma mark-table View data source-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {#warni
Ng potentially incomplete method implementation.
return 1; }-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {#warning incomplete method implementation.
return [Tabledata Count]; }-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {//Designated cell
Identifier is a custom cell static nsstring *cellidentifier = @ "Tableviewcell";
Custom cell class Tableviewcell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier]; if (cell = = nil) {//load the custom cell cell by the name of Xib = [[[NSBundle Mainbundle] loadnibnamed:@ "Tableviewcell" owner:self
Options:nil] Lastobject];
///Add test data Cell.titleLabel.text = [Tabledata ObjectAtIndex:indexPath.row];
Cell.content.text = @ "This is some test data";
Test picture cell.iamge.image = [UIImage imagenamed:@ "testimage.jpg"];
return cell; #pragma mark-table View delegate-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath
*) Indexpath {} @end
Final effect:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.