UITableView Sectionheader the header of a custom section

Source: Internet
Author: User

Customizing the header of a section
-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) Section
{
UIView *headerview = [[UIView alloc] Initwithframe:cgrectmake (10, 0, 300, 30)];//Create a view
Uiimageview *headerimageview = [[Uiimageview alloc] Initwithframe:cgrectmake (10, 0, 300, 30)];
UIImage *image = [UIImage imagenamed:@ "4-2.png"];
[Headerimageview Setimage:image];
[Headerview Addsubview:headerimageview];
[Headerimageview release];

NSString *createtime = [Self.keysarray objectatindex:section];
Createtime = [Createtime stringbyreplacingcharactersinrange:nsmakerange (4, 1) withstring:@ "-"];
Createtime = [Createtime stringbyreplacingcharactersinrange:nsmakerange (7, 1) withstring:@ "-"];

UILabel *headerlabel = [[UILabel alloc] Initwithframe:cgrectmake (130, 5, 150, 20)];
Headerlabel.backgroundcolor = [Uicolor Clearcolor];
Headerlabel.font = [Uifont boldsystemfontofsize:15.0];
Headerlabel.textcolor = [Uicolor Bluecolor];
Headerlabel.text = Createtime;
[Headerview Addsubview:headerlabel];
[Headerlabel release];

return headerview;
}

set UITableView section, cell background colorCategory: Iosui 2012-05-30 11:24 746 people read comments (0) favorite reports Uiviewtableheader

The gray background and white font shown in section are the default, and can be implemented by calling the following methods

-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) Section {
return [Self.keys objectatindex:section];
}

If you want to change the background and font here, the official does not open the interface to directly modify the above two properties, so, only the label itself, plus view to implement, the code is as follows:

Implementing the Delegate Method-(UIView *) TableView: (uitableview *)tableView viewforheaderinsection: (nsinteger) Section


-(UIView *) TableView: (UITableView *) TableView viewforheaderinsection: (nsinteger) Section
{
uiview* MyView = [[[[UIView alloc] init] autorelease];
Myview.backgroundcolor = [Uicolor colorwithred:0.10 Green:0.68 Blue:0.94 Alpha:
UILabel *titlelabel = [[UILabel] Alloc] Initwithframe:cgrectmake (10, 90, 22)];
Titlelabel.textcolor=[uicolor Whitecolor];
Titlelabel.text=[self.keys Objectatindex:section];
[MyView Addsubview:titlelabel];
[Titlelabel release];
return myview;
}

The button background color provided by the cocoa is transparent. Because Contentview is removed, the following is the color of TableView, which is not part of the cell.
So the best way to do this is to change the cell's background by Cell.backgroundview. According to the documentation, Backgroundview is always at the bottom of the cell, so the other subview background in the cell is set to [Uicolor Clearcolor], To Cell.backgroundview as a unified background, should be the best way. UIView *backgrdview =[[uiview alloc] initWithFrame:cell.frame];
Backgrdview.backgroundcolor=[uicolor Bluecolor];
Cell.backgroundview= Backgrdview;
[Backgrdview release];

In -(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath method, [TableView Deselectrowatindexpath:[tableview Indexpathforselectedrow] Animated:yes]iOS dynamic tuning list row high lessons

The initial list interface (UITableView) row height is fixed. So the implementation of uitableviewdelegate in:

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath

Returns a fixed number of cgfloat types available.

Not long ago, it was necessary to dynamically adjust the height of each cell inside the UITableView. (Inside the cell is a

uilable), the uilable.text length is variable.

Finally passed, in

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath

Called in

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath

Get the UITableViewCell, and finally by returning to UITableViewCell.frame.size.height fix

This is probably the reason:

Reload calls Cellforrowatindexpath, but it does this before Heightforrowatindexpath

All calls to Cellforrowatindexpath inside the Heightforrowatindexpath are not problematic.

One obvious problem here is that Cellforrowatindexpath will be called two times.

There is another requirement today: the need for a certain cell to be uitableview after a change in datasource
Reload (reloadrowsatindexpaths). After reload, you need to add some vertical arrangement sub-views (addsubview) to the cell and have the Subview in the viewable area.

In the Reload section. Cellforrowatindexpath section. (Reload automatically calls Cellforrowatindexpath) adds scroll-related code for some uiscrollview. (UITableView inherits from Uiscrollview). The cellforrowatindexpath was found to be called by loop.

It's too much for me.

The source code for iOS is not visible, but it should feel like the following sequence of calls:

Cellforrowatindexpath calls Uiscrollview's scroll-related code. And Uiscrollview's scroll-related code calls Heightforrowatindexpath. Heightforrowatindexpath will call Cellforrowatindexpath again.

A complete cycle of death has arisen.

All, when the TableView line high, million can not be easily heightforrowatindexpath call

Cellforrowatindexpath to get the cell-related height. Otherwise the consequences are too serious.

The final approach remains to be solved by calculating the actual height of the cell. may be more complex. Also more trouble. But it is a more secure approach. There is no effect on the later expansion of new functionality.

One thing to note: The size of the returned view in this method is fixed

The table view automatically adjusts the height of the section header to accommodate the returned view object. The table view does not call this method if it is created in a plain style (uitableviewstyleplain).


UITableView Sectionheader the header of a custom section

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.