0

当我单击 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 代码。

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

我能做些什么?

如何做到这一点,以便当我选择一个单元格时它不会变得更粗或更锐利?谢谢。

4

3 回答 3

1

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

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

当您描述触摸会增加字体大小时,我假设您会这样做[tableview reloadData];在tableView:didSelectRowAtIndexPath:.

于 2013-06-16T19:34:46.737 回答
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];
    }

}
于 2013-06-16T18:50:41.517 回答
0

尝试这样的事情。

- (void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)index
{
    UITableViewCell *cell = [tableview cellForRowAtIndexPath:index];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14.0]; 
}
于 2013-06-16T18:27:21.643 回答