Two built-in uitableview styles
Uitableviewstyleplain
Uitableviewstylegrouped
The uitableviewdatasource protocol must implement two methods: cellforrowatlndexpath and numberofrowsinsection.
/*
Display two groups of data
Guangdong: Guangzhou, Shenzhen, Meizhou, and Dongguan
Hunan: Changsha, Yiyang, Yueyang
Hubei: Huanggang, Wuhan
*/
@ Interface viewcontroller () <uitableviewdatasource>
{
Nsarray * _ allcities;
}
@ End
@ Implementation viewcontroller
-(Void) viewdidload {
[Super viewdidload];
// 1. Add uitableview
Uitableview * tableview = [[uitableview alloc] initwithframe: Self. View. bounds style: uitableviewstylegrouped];
Tableview. datasource = self;
[Self. View addsubview: tableview];
_ Allcities = @[
@ [@ "Guangzhou", @ "Shenzhen", @ "Meizhou", @ "Dongguan"],
@ [@ "Changsha", @ "Yiyang", @ "Yueyang"],
@ [@ "Huanggang", @ "Wuhan"]
];
}
# Pragma mark-Data Source Method
# How many groups of Pragma mark (Section = region \ group)
-(Nsinteger) numberofsectionsintableview :( uitableview *) tableview
{
Return _ allcities. count;
}
# Total number of lines in The Pragma mark Section
// Nsinteger type of the input section, starting from 0. 0 indicates the first group.
-(Nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section
{
Nsarray * sectionarray = _ allcities [section];
Return sectionarray. count;
}
# Pragma Mark returns the content displayed for each row (what cell is displayed for each row)
-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath
{
// Indexpath identifies a unique row
// Row of the section group
// Indexpath. Row
// Indexpath. Section
Uitableviewcell * cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: Nil];
// Set the text displayed by the Cell
Cell. textlabel. Text = _ allcities [indexpath. Section] [indexpath. Row];
Return cell;
}
# Title of the header displayed in the section group of Pragma mark
-(Nsstring *) tableview :( uitableview *) tableview titleforheaderinsection :( nsinteger) Section
{
Return @ "hahah ";
}
# The ending title of the section group of The Pragma mark
-(Nsstring *) tableview :( uitableview *) tableview titleforfooterinsection :( nsinteger) Section
{
Return @ "ooo ";
}
@ End
Rebuilding with dictionaries
# Import "viewcontroller. H"
# Define kheader @ "Header"
# Define kfooter @ "footer"
# Define kcities @ "Cities"
@ Interface viewcontroller () <uitableviewdatasource>
{
Nsarray * _ allprovinces; // all provinces
}
@ End
@ Implementation viewcontroller
-(Void) viewdidload {
[Super viewdidload];
// 1. Add uitableview
Uitableview * tableview = [[uitableview alloc] initwithframe: Self. View. bounds style: uitableviewstylegrouped];
Tableview. datasource = self;
[Self. View addsubview: tableview];
_ Allprovinces = @[
@{
Kheader: @ "Guangdong ",
Kfooter: @ "Guangdong Hao ",
Kcities: @ [@ "Guangzhou", @ "Shenzhen", @ "Meizhou", @ "Dongguan"]
},
@{
Kheader: @ "Hubei ",
Kfooter: @ "Guangdong is also good ",
Kcities: @ [@ "Changsha", @ "Yiyang", @ "Yueyang"]
}
];
}
# Pragma mark-Data Source Method
# How many groups of Pragma mark (Section = region \ group)
-(Nsinteger) numberofsectionsintableview :( uitableview *) tableview
{
Return _ allprovinces. count;
}
# Total number of lines in The Pragma mark Section
// Nsinteger type of the input section, starting from 0. 0 indicates the first group.
-(Nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section
{
Nsarray * sectionarray = _ allprovinces [section] [kcities];
Return sectionarray. count;
}
# Pragma Mark returns the content displayed for each row (what cell is displayed for each row)
-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath
{
// Indexpath identifies a unique row
// Row of the section group
// Indexpath. Row
// Indexpath. Section
Uitableviewcell * cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: Nil];
// Set the text displayed by the Cell
Cell. textlabel. Text = _ allprovinces [indexpath. Section] [kcities] [indexpath. Row];
Return cell;
}
# Title of the header displayed in the section group of Pragma mark
-(Nsstring *) tableview :( uitableview *) tableview titleforheaderinsection :( nsinteger) Section
{
Return _ allprovinces [section] [kheader];
}
# The ending title of the section group of The Pragma mark
-(Nsstring *) tableview :( uitableview *) tableview titleforfooterinsection :( nsinteger) Section
{
Return _ allprovinces [section] [kfooter];
}
@ End
You can also use the model for code refactoring, which will be more convenient (the same as in C ), colleagues provide a class method to initialize the data. uitableview Summary 1. Data Display conditions-"all data in uitableview is provided by the data source. Therefore, to present data in uitableview, the datasource data source object of uitableview must be set. The datasource object of uitableview must comply with the uitableviewdatasource Protocol to implement the corresponding data source method. When uitableview wants to display data, A message is sent to the data source (the data source method is called). uitableview determines the data to be displayed based on the return value of the method. 2. The data presentation process is described in "Call the data source first ".
-(Nsinteger) numberofsectionsintableview :( uitableview *) tableview
How many groups are there?
-And then call
-(Nsinteger) tableview :( uitableview *) tableview numberofrowsinsection :( nsinteger) Section
Know the total number of rows in the Section
-And then call
-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath
// Indexpath identifies a unique row
// Row of the section group
// Indexpath. Row
// Indexpath. Section
-What is the header title displayed in the section group?
-(Nsstring *) tableview :( uitableview *) tableview titleforheaderinsection :( nsinteger) Section
-What is the tail title displayed in the section group?
-(Nsstring *) tableview :( uitableview *) tableview titleforfooterinsection :( nsinteger) Section
Show index entries
# Pragma Mark returns the index entries on the right of the table
-(Nsarray *) sectionindextitlesfortableview :( uitableview *) tableview
{
// The returned content is not required. During the click operation, the uitableviewcell at the corresponding position is displayed based on the position of the clicked data in the array.
Return @ [@ "1", @ "2", @ "3", @ "4", @ "5"];
}
Pinyin processing database pinyin4objc
Develop Advanced 13_uitableview multi-line display