当我点击一个uitableview单元格时,该单元格的文本会变得更大胆?

时间:2013-06-16 18:21:46

标签: ios objective-c uitableview

当我点击一个uitableview单元格时,该单元格的文本变得更大胆。我该怎么办?

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    UIView * selected = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 320, 100)];
    [selected setBackgroundColor: [UIColor colorWithRed: 234/255.0f green: 234/255.0f  blue: 234/255.0f alpha: 1.0]];
    cell.selectedBackgroundView = selected;

    NSDictionary * val = [self.entries objectAtIndex: indexPath.row];

    UIView * cellview = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 320, 85)];
    [cellview setBackgroundColor: [UIColor whiteColor]];

    NSString * namet = @"";
    NSString * pitcht = @"";
    NSString * category = @"";
    NSString * first_name = @"";
    NSString * last_name = @"";
    NSString * photo = @"";

    NSDictionary * investor;
    NSDictionary * startup;
    float miles = 0;

    NSDictionary * user;

    if(showInvestors)
    {
        if ([val objectForKey: @"investor"] &&
            [val objectForKey: @"investor"]!= (id)[NSNull null])
        {
            investor = [val objectForKey: @"investor"];

            if ([investor objectForKey: @"user"] &&
                [investor objectForKey: @"user"] != (id)[NSNull null])
            {
                user = [investor objectForKey: @"user"];

                first_name = [user objectForKey: @"first_name"];
                last_name = [user objectForKey: @"last_name"];

                namet = [NSString stringWithFormat: @"%@ %@", first_name, last_name];
            }

            if ([investor objectForKey: @"bio"] &&
                [investor objectForKey: @"bio"] != (id)[NSNull null])
            {
                pitcht = [investor objectForKey: @"bio"];
            }

            if ([investor objectForKey: @"category_list"] &&
                [investor objectForKey: @"category_list"] != (id)[NSNull null])
            {
                category =  [investor objectForKey: @"category_list"];
            }


            if ([investor objectForKey: @"photo_url"] &&
                [investor objectForKey: @"photo_url"] != (id)[NSNull null])
            {
                photo =  [investor objectForKey: @"photo_url"];
            }
        }

    }
    else
    {

        if ([val objectForKey: @"startup"] &&
            [val objectForKey: @"startup"] != (id)[NSNull null])
        {
            startup = [val objectForKey: @"startup"];
        }


        if ([startup objectForKey: @"name"] &&
            [startup objectForKey: @"name"] != (id)[NSNull null])
        {
            namet = [startup objectForKey: @"name"];
        }


        if ([startup objectForKey: @"pitch"] &&
            [startup objectForKey: @"pitch"] != (id)[NSNull null])
        {
            pitcht = [startup objectForKey: @"pitch"];
        }

        if ([startup objectForKey: @"categories"] &&
            [startup objectForKey: @"categories"]  != (id)[NSNull null] &&
            [[[startup objectForKey: @"categories"] objectAtIndex: 0] objectForKey: @"name"] &&
            [[[startup objectForKey: @"categories"] objectAtIndex: 0] objectForKey: @"name"] != (id)[NSNull null])
        {
            category = [[[startup objectForKey: @"categories"] objectAtIndex: 0] objectForKey: @"name"];
        }

        if ([startup objectForKey: @"photo_url"] &&
            [startup objectForKey: @"photo_url"] != (id)[NSNull null])
        {
            photo =  [investor objectForKey: @"photo_url"];
        }
    }


    UIImageView * midview = [[UIImageView alloc] initWithFrame: CGRectMake(10, 15, 50, 50)];

    if(photo.length >= 1)
    {
        NSURL *url = [[NSURL alloc] initWithString: photo];
        [midview setImageWithURL:url placeholderImage:[UIImage imageNamed:@"avatar"]];
        [midview.image imageCroppedToFitSize: CGSizeMake(60, 60)];
    }
    [cellview addSubview: midview];

    UILabel * name = [[UILabel alloc] initWithFrame: CGRectMake(70, 15, 150, 16)];
    [name setText: namet];
    [name setFont: [UIFont fontWithName: @"HelveticaNeue-Bold" size: 16.0f]];
    [name setTextColor: [UIColor colorWithRed: 79.0/255.0f green: 79.0/255.0f blue: 79.0/255.0f alpha: 1.0]];

    UILabel * pitch = [[UILabel alloc] initWithFrame: CGRectMake(70, 31, 200, 26)];
    [pitch setFont: [UIFont fontWithName: @"HelveticaNeue-Light" size: 12.0f]];
    [pitch setText:  pitcht];
    pitch.numberOfLines = 1;
    [pitch setTextColor: [UIColor colorWithRed: 79.0/255.0f green: 79.0/255.0f blue: 79.0/255.0f alpha: 1.0]];

    UIImage * play = [UIImage imageNamed: @"e_play.png"];
    UIImageView * playview = [[UIImageView alloc] initWithImage: play];
    [playview setFrame: CGRectMake(280, 20, play.size.width, play.size.height)];
    [cellview addSubview: name];
    [cellview addSubview: pitch];
    [cellview addSubview: playview];

    NSString * catstr = [NSString stringWithFormat: @"Industry: %@", category];
    UILabel * catlb = [[UILabel alloc] initWithFrame: CGRectMake(70, 58, 180, 14)];
    [catlb setFont: [UIFont fontWithName: @"HelveticaNeue-Light" size: 12.0f]];
    [catlb setText:  catstr];
    [catlb setTextColor: [UIColor colorWithRed: 79.0/255.0f green: 79.0/255.0f blue: 79.0/255.0f alpha: 1.0]];
    [cellview addSubview: catlb];


    if ([val objectForKey: @"latitude"] &&
        [val objectForKey: @"latitude"] != (id)[NSNull null] &&
        [val objectForKey: @"longitude"] &&
        [val objectForKey: @"longitude"] != (id)[NSNull null])
    {

        CLLocationDegrees slatitude = [[val objectForKey: @"latitude"] doubleValue];
        CLLocationDegrees slongitude = [[val objectForKey: @"longitude"] doubleValue];

        CLLocationDistance distance = [self myDistanceFromLatitude: slatitude longitude: slongitude];

        if(distance > 0)
        {
            miles = [self convertToMiles: distance];
        }
    }

    NSString * distancetxt = [NSString stringWithFormat: @"%1.1f miles", miles];

    UILabel * dislb = [[UILabel alloc] initWithFrame: CGRectMake(220, 15, 50, 16)];
    [dislb setFont: [UIFont fontWithName: @"HelveticaNeue-Light" size: 11.0f]];
    [dislb setText:  distancetxt];
    [dislb sizeToFit];
    [dislb setTextColor: [UIColor colorWithRed: 79.0/255.0f green: 79.0/255.0f blue: 79.0/255.0f alpha: 1.0]];
    [cellview addSubview: dislb];


    [cell addSubview: cellview];

    return cell;

这是我上面的cellForRowAtIndexPath代码。

当我点击一个单元格时,单元格的文本变得更清晰......

我该怎么办?

如何制作它以便在选择单元格时不会变得更大胆或更锐利?感谢。

3 个答案:

答案 0 :(得分:1)

每次重新绘制细胞时,都会添加UILabel(和图像视图)。这不是你想要的。 Yi想要在现有标签上设置文本 - 但是您将标签堆叠在一起。

您应该创建一个自定义UITableViewCell子类。

正如您所描述的那样,触摸增加了字体大小,我假设你做[tableview reloadData];在tableView:didSelectRowAtIndexPath:

答案 1 :(得分:0)

尝试类似这样的事情。

- (void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)index
{
    UITableViewCell *cell = [tableview cellForRowAtIndexPath:index];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0]; 
}

答案 2 :(得分:0)

您还可以对UITableViewCell进行子类化并覆盖方法

- (void) setSelected: (BOOL) selected
            animated: (BOOL) animated
{
    [super setSelected: selected
              animated: animated];

    if (selected)
    {
        cell.textLabel.font = [UIFont boldSystemFontOfSize: 14.0];
    }
    else
    {
        cell.textLabel.font = [UIFont systemFontOfSize: 14.0];
    }

}