ios - 当我单击 uitableview 单元格时,该单元格的文本变得更粗?

标签 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 代码。

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

我能做什么?

如何才能在选择单元格时使其不会变得更粗或更清晰?谢谢。

最佳答案

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

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

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

关于ios - 当我单击 uitableview 单元格时,该单元格的文本变得更粗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17136313/

相关文章:

ios - Swift:相机 View 上的圆角

javascript - iOS 上的 JQ UI 可拖动 : initiating dragging in taphold-handler

iphone - 在Google JSON之外获取邮政编码

ipad - 行比 UITableView 本身更窄

ios - 从自定义 Cell 中正确委托(delegate)按钮操作以删除 UITableView 中的行

ios - 任何获取iOS系统默认主题颜色的方法

ios - 如何使用 Core Animation 为脉动的蓝点制作动画?

objective-c - __cxa_throw 在 fontWithName :size:

objective-c - NSOutlineView + NSTreeController + CoreData + Bindings 行重复

ios - Swift:为 UITableViewController 子类使用变量