问题

Hi friends. i Need to display the labels in a UItableView. How can i do that. Kindly refer the screenshot.
回答1:
You can use the UITableViewCellStyleValue1
style of UITableViewCell. Use Custom view for the section header.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 22.0f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UILabel *sectionLabel = [[UILabel alloc]initWithFrame:CGRectZero];
sectionLabel.backgroundColor = [UIColor purpleColor];//Choose your color
sectionLabel.textColor = [UIColor whiteColor];
sectionLabel.font = [UIFont boldSystemFontOfSize:17.0f];
sectionLabel.text = @"Section Name";
return sectionLabel;
}
- (UITableViewCell *)tableView:(UITableView *)TableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
//Default detailTextLabel would have blue text color change it to your choice
cell.detailTextLabel.textColor = [UIColor darkGrayColor];
}
cell.textLabel.text = @"Mobile Number";
cell.detailTextLabel.text = @"Type";
return cell;
}
回答2:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellsToBeReused = @"CellsToBeReused";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellsToBeReused];
if (cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellsToBeReused] autorelease];
}
UILabel* Label = [[UILabel alloc] initWithFrame:CGRectMake(2,2, 62, 23)];
[Label setText:@"Text"];
Label.backgroundColor = [UIColor whiteColor];
[cell.contentView addSubview:Label];
[Label release];
return cell;
}
回答3:
UITableViewDelegate has a method -
(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
You could simply return a customized UILabel
https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html
回答4:
Its better you go thorough with UITableView. Apple is the best resource
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/ManageSelections/ManageSelections.html
Hope this helps !!!
回答5:
UITableViewCell
objects have a contentView
property. Add any custom views as subviews
of contentView
.
What you want is a Custom UITableViewCell
. If you Google for that, you'll find a lot of Tutorials and Information.
For example :
- Custom UITableViewCell in IB.
- Custom UITableViewCell Using Interface Builder.
来源:https://stackoverflow.com/questions/17142239/displaying-labels-with-different-background-color-in-grouped-tableview