UITableView is almost the most useful control in iOS development and, of course, a control that has quite a few things to remember.
Create
first create a new project and add a Mainviewcontroller class file
Open MainViewController.h File
@interface mainviewcontroller:uiviewcontroller<uitableviewdatasource,uitableviewdelegate>
@property ( Nonatomic, retain) Nsarray *datalist;
@property (nonatomic, retain) UITableView *mytableview;
@end
The TableView data source Uitableviewdatasource.
TableView commissioned by Uitableviewdelegate.
If the current class is inherited from Uiviewcontroller, you need to add the above code, and if you inherit directly from Uitableviewcontroller you do not need to add
Then hit the mainviewcontroller.m file, initialize the UITableView and display in the current window
-(void) viewdidload
{
[super viewdidload];
Initialize TableView data
nsarray *list = [Nsarray arraywithobjects:@ "Wuhan", @ "Shanghai", @ "Beijing", @ "Shenzhen", @ "Guangzhou", @ "Chongqing", @ "Hong Kong", @ "Taiwan Strait", @ " Tianjin ", nil];
self.datalist = list;
UITableView *tableview = [[[UITableView alloc] InitWithFrame:self.view.frame Style:uitableviewstyleplain] Autorelease ];
Sets the TableView data source
tableview.datasource = self;
Set the TableView delegate
tableview.delegate = self;
Sets the background of the tableview
tableview.backgroundview = [[Uiimageview alloc] Initwithimage:[uiimage imagenamed:@ " Background.png "]];
Self.mytableview = TableView;
[Self.view Addsubview:mytableview];
}
When initializing, you can set the style for TableView
The first: List of Uitableviewstyleplain
The second type: group uitableviewstylegrouped
Create and set the content displayed per line
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath
{
static NSString *cellwithidentifier = @ "Cell";
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellwithidentifier];
if (cell = = nil) {
cell = [[UITableViewCell alloc] initwithstyle:uitableviewcellstylevalue2 reuseidentifier: Cellwithidentifier];
}
Nsuinteger row = [Indexpath row];
Cell.textLabel.text = [Self.datalist objectatindex:row];
Cell.imageView.image = [UIImage imagenamed:@ "Green.png"];
Cell.detailTextLabel.text = @ "Detailed information";
return cell;
}
UITableViewCell style can also be set, if you do not meet the needs of the project, you can define the UITableViewCell style
Uitableviewcellstyledefault
Uitableviewcellstylesubtitle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
The grouped tableview can also be segmented by the following method, where the number 1 is divided into 1 segments
-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView
{return
1;
}
Set content Indent
-(Nsinteger) TableView: (UITableView *) TableView Indentationlevelforrowatindexpath: (Nsindexpath *) IndexPath
{ return
[Indexpath row];
}
Set the row height of the cell
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath
{
return ;
}
Set the alternate color of a cell
-(void) TableView: (UITableView *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: (NSIndexPath *) Indexpath
{
if ([Indexpath row]% 2 = 0) {
cell.backgroundcolor = [Uicolor bluecolor];
} else {
cell. BackgroundColor = [Uicolor greencolor];
}
When the specified cell is selected, pop-up uialertview display the selected content
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
{
NSString * msg = [[NSString alloc] initwithformat:@ "You chose:%@", [self.datalist Objectatindex:[indexpath Row]];
Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Prompt" message:msg delegate:self cancelbuttontitle:@ "OK" Otherbuttontitles:nil, nil];
[MSG release];
[Alert show];
}
Slide selected row After delete
-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath
{
NSLog (@ "perform delete operation");
}
UITableView Refresh:
[Self.tableview Reloaddata];
Reloaddata is refreshing the entire uitableview, and sometimes we may need to refresh locally. For example: Only a cell refresh, only one section, and so on. This time in the call Reloaddata method, although the user can not see, but some waste of resources.
Refresh Local Cell:
Copy Code code as follows:
Nsindexpath *indexpath = [Nsindexpath indexpathforrow:0 insection:0];
[Self.tableview Reloadrowsatindexpaths:[nsarray Arraywithobjects:indexpath,nil] WithRowAnimation: Uitableviewrowanimationfade];
This makes it easy to refresh the first cell in the first section. Although it seems to be a lot of code, it does compare to saving resources. As little as possible refresh, is also a uitableview optimization.
Partial Refresh section:
Nsindexset *indexset = [[Nsindexset alloc] initwithindex:0];
[Self.tableview Reloadsections:indexset Withrowanimation:uitableviewrowanimationfade];
The above code is to refresh the No. 0 section.
To refresh the animation:
Refresh UITableView There are several more animations:
typedef ns_enum (Nsinteger, uitableviewrowanimation) {
uitableviewrowanimationfade,//Fade
in Uitableviewrowanimationright,//sliding from right into //slide in/off
to right-hand uitableviewrowanimationleft,// Slide from left
to Uitableviewrowanimationtop, //from top to
uitableviewrowanimationbottom,//from slide into
Uitableviewrowanimationnone,// available in iOS 3.0
uitableviewrowanimationmiddle,// available in iOS 3.2. Attempts to keep cell centered into the space it will/did occupy uitableviewrowanimationautomatic
= m//Availab Le in IOS 5.0. Chooses an appropriate animation style for you
};