天天看點

為什麼使用viewWithTag來通路Subview

為什麼使用viewWithTag來通路Subview

http://www.cnblogs.com/procoder/archive/2012/12/29/Why-use-viewWithTag.html

在UITableViewCell裡面,我們可以通過viewWithTag方法來通路Subview,這是一種快速但是比較邋遢的方式。這種方法的好處是可以快速的通路一堆連号的控件(subview),而不需要一個個的通過Ctrl drag來設定IBOutlet。下面是根據NSMutableArray設定UI的代碼,好用。

int i = 0;

    for(id number in draw.primaryNumbers){

        UILabel *n = (UILabel *)[cellviewWithTag:300+i];

        // NSLog(@"%@", n);

        n.text = number;

        NSString* imageFile = [NSStringstringWithFormat:@"Images/ball.png"];

        UIImageView * imageBall = (UIImageView *)[cellviewWithTag:200 + i];

        imageBall.image = [UIImageimageNamed:imageFile];

        ++i;

    }

使用loadHTMLString:baseURL來加載UIWebView

//在頁面上已設定UIWebView的tag=120。裡面的product是一個NSDictionary。

UIWebView *imageView = (UIWebView *)[cell viewWithTag:120];

NSString *imageHTMLString = [[NSString alloc] initWithFormat:@"<body style='margin:0px; padding:0px'><img height='95' src='%@'/></body>",

[product objectForKey:@"ProductImage"]];

[imageView loadHTMLString:imageHTMLString baseURL:nil];

繼續閱讀