#iOS开发笔记 #ios6 and below, Cell.backgroundcolor set the cause of the failure

Source: Internet
Author: User

When implementing UITableView in iOS development, it is generally in the delegate method [Uitableviewdatasource Tableview:cellforrowatindexpath:] Initializes each cell in the table, and so on. According to the general logic, the backgroundcolor of the cell also needs to be done here, and the code that implements it is often this:

<span style= "FONT-SIZE:14PX;" >-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {
    static NSString *cellidentifier = @ "Menucell";
    UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];
    if (cell = = nil) {
        cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier: Cellidentifier];
    }

    ...

    Cell.backgroundcolor = [Uicolor whitecolor];
    
    ...

    return cell;
} </span>

But after a simple test, it turns out that there are no problems with iOS 7 and above, BackgroundColor set normally, but not working in systems like iOS 6 and below. The backgroundcolor of the cell always stays the same as the TableView. What's going on. Is it a bug in iOS?

To explore this, I first tried to inherit a uitableview subclass and then rewrite the-setbackgroundcolor method.

<span style= "FONT-SIZE:14PX;" >-(void) SetBackgroundColor: (Uicolor *) backgroundcolor {
    if (ISGREATERTHANIOS7) {                        //current system is iOS 7 and above
        [ Super Setbackgroundcolor:backgroundcolor];
    } else {                                        //ios 6 and below
        uiview *backgroundview = [[UIView alloc] initWithFrame:self.bounds];
        Backgroundview.backgroundcolor = BackgroundColor;
        Self.backgroundview = Backgroundview;
    }
</span>

But the strange thing is, even if I do this backgroundview the cell, the Tao sound still ... And I found that for each cell, it would invoke multiple-setbackgroundcolor methods, and only the first time the backgroundcolor was the one I wrote, and then the same color as the tableview background several times. It is the color that it automatically sets.

To verify my conjecture, I went to the official iOS document and found a passage in UITableViewCell's introduction: (Original address: https://developer.apple.com/library/prerelease/ios/ documentation/uikit/reference/uitableviewcell_class/index.html)

Whether a predefined or custom cell, you can change the cell's background using the Backgroundview property or by Changing the inherited BackgroundColor property. In IOS 7, cells have a white background by default; In earlier versions of IOS, cells inherit the background color of the enclosing table view. If you want to change the background color of a cell, does so in the tableView:willDisplayCell:forRowAtIndexPath:method of Your table View delegate.

Omg...... Finally found the problem, no wonder rewrite the set method is useless, the original iOS 7 earlier versions, the cell will inherit the TableView background color by default. Of course the Apple solution is to set up in [Uitableviewdelegate-tableview:willdisplaycell:forrowatindexpath:], the code is simple:

<span style= "FONT-SIZE:14PX;" >-(void) TableView: (UITableView *) TableView Willdisplaycell: (UITableViewCell *) cell Forrowatindexpath: ( Nsindexpath *) Indexpath {
    Cell.backgroundcolor = [Uicolor whitecolor];
} </span>
Test with different versions of the system and solve the problem.


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.