Displaying labels with different background color in grouped tableView ? [closed]

怎甘沉沦 提交于 2019-12-13 23:23:23

问题


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 :

  1. Custom UITableViewCell in IB.
  2. Custom UITableViewCell Using Interface Builder.


来源:https://stackoverflow.com/questions/17142239/displaying-labels-with-different-background-color-in-grouped-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!