A little pit in the UITableView about Viewforheaderinsection

Source: Internet
Author: User

Beginners, in the premise of maintaining interest, the most straightforward way is to follow the example of manually knocking over the code, and then run the example, so that the most intuitive to see the effect.

However, the general beginner-oriented tutorials or examples, technical aspects are relatively simple, the implementation of the effect is relatively low, like the developer just built the house, also called the House, but that is the rough room. Compared with other language development experience or project experience for beginners (Ps: For example, I ~), often will vitalize, will take the initiative to explore the tutorial related to the "posture Point", to try to use the new skills already mastered to improve these instances, expand, want to make a fine decoration of the house. And because of a new contact with the language, not enough to understand the depth, it will inevitably fall into a small pit.

The two-day example is the encoding implementation of the UITableView, or the same old, do not drag the control, do not use the Auto layout layout, requirements compatible with Apple's different resolutions of three devices, temporarily do not consider the ipad.

The function is very simple, it is easy to get out. But the table head is ugly, looking very uncomfortable, wondering whether to make it look good.

// Normal Set Header -(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (Nsinteger) section{     return @" Infant Milk Powder brand list (default header) " ;}

Such as:

What I want to do is to make the header look nice and add a little bit of customization, like adding a little icon, adding a background color or something.

//Customizing the table header-(uiview*) TableView: (UITableView *) _tableview viewforheaderinsection: (nsinteger) Section {UIView*headerview = [[UIView alloc] Initwithframe:cgrectmake (0,0, _tableview.frame.size.width, -)]; //Left iconUiimageview *icon = [[Uiimageview alloc] Initwithframe:cgrectmake (5,5, +, +)]; Icon.image= [UIImage imagenamed:@"250x250"];//Right textUILabel *label = [[UILabel alloc] Initwithframe:cgrectmake ( $,0, _tableview.frame.size.width- -, -)]; Label.textcolor=[Uicolor Orangecolor]; Label.text=@"Infant Milk Powder brand list (custom header)"; Label.textalignment= Nstextalignmentcenter;//message mode set background color (Uicolor last to Cgcolor type)[Headerview.layer setbackgroundcolor:[uicolor colorwithred:230.0/255.0Green230.0/255.0Blue250.0/255.0Alpha1].    Cgcolor]; //Set Background Color by property mode//Headerview.backgroundcolor = [Uicolor colorwithred:230.0/255.0 green:230.0/255.0 blue:250.0/255.0 alpha:1];[Headerview Addsubview:icon];        [Headerview Addsubview:label]; returnHeaderview;}

Originally thought to here, almost achieved the effect I want: The table head to the left there is a small icon, the right is a text description. But it is not come out I want the effect, do not know what the problem. In the custom header method inside the log, found that the program did not go in ...

Novice, all kinds of Google, find a lot of information and finally found a similar problem on the stackoverflow.com, to read the official documents to understand what is going on.

Know what the problem is good to do, honestly to achieve Tableview:heightforheaderinsection Bai

// sets the height of the table header. If you use a custom header, the method must be implemented, otherwise the custom header cannot be executed, and no error is available-(cgfloat) TableView: (UITableView *) TableView Heightforheaderinsection: (nsinteger) section{    return;}

This is good, I want the effect come out ~

A little pit in the UITableView about Viewforheaderinsection

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.