How would开发者_如何学编程 I change the color of a UITableView? Or change the color of a cell? Or change the color of the navigation bar at the top? (iOS)
You can change the color of a cell in the UITableView delegate method like this:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:@"UITableViewCell"]autorelease];
}
}
//Here you can change de color/background of your cell with anything you want
//Change the color:
[cell setBackgroundColor:[UIColor blackColor]];
//Change the background image of a cell:
UIImage *theImage = [UIImage imageNamed:@"ximage.png"];
[[cell imageView] setImage:theImage];
}
To change the color of a cell you need to create your own subclass of UITableViewCell. And to change the background color of the nav bar, I believe it is
self.navigationItem.backgroundColor = [UIColor redColor];
Thanks,
macintosh264
You can do so by setting the backgroundView to the table and the contentView's backgroundColor of a cell. See this to get started with. The navigation bar can be done by subclassing the UINavigationBar and overriding the drawRect method.
P.S: All of the above are basic topics on which tutorials are littered all over the web and Stack Overflow itself. Try searching and reading up.
note that as of ios3 you don't set the cell background color in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
you set it in
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
as noted here
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html
精彩评论