iphone - 如何将星级图像放置在文本标签旁边

标签 iphone objective-c ios xcode ipad

我正在解析 json feed,并根据解析的值将星级图像放置在 TableView 单元格中。我想要的是一旦文本标签中的单词数完成,星级图像就放置星级图像灵活,没有固定的框架。下面是输出的代码和屏幕截图。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

static NSString *CellIdentifier = @"Cell";
HJManagedImageV* mi;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    mi = [[[HJManagedImageV alloc] initWithFrame:CGRectMake(-18,-2,90,90)] autorelease];
    mi.tag = 999;
    [cell addSubview:mi];
}
else
{
    mi=(HJManagedImageV *)[cell viewWithTag:999];
    [mi clear];
}

NSDictionary *boy=[self.media1 objectAtIndex:indexPath.row];
NSString *str=[[NSString alloc]initWithFormat:@"%@",boy];
NSInteger n=[str intValue];
NSLog(@"the value:%@",str);

if(n ==0)
{   
    CGRect starFrame = CGRectMake(100,6, 85, 40);

    UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
    starImage.image = [UIImage imageNamed:@"nostar.png"];
    starImage.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:starImage];

    NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
    NSString *str=[[NSString alloc]initWithFormat:@"(%@)",boo];
    CGRect labelFrame = CGRectMake(290,18, 40, 40);
    UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];

    Label.text=str;
    Label.tag=1000;
  //  Label.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:Label];


}
if(n >=1)
{   
    CGRect starFrame = CGRectMake(100,6, 85, 40);

    UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
    starImage.image = [UIImage imageNamed:@"1star.png"];
    [cell.contentView addSubview:starImage];

    NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
    CGRect labelFrame = CGRectMake(290,18, 40, 40);
    UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
    NSString *str=[[NSString alloc]initWithFormat:@"(%@)",boo];
    Label.text=str;
    Label.tag=1000;
   //Label.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:Label];

}

if(n >=2)
{   
    CGRect starFrame = CGRectMake(100,6, 85, 40);

    UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
    starImage.image = [UIImage imageNamed:@"twostar.png"];
    [cell.contentView addSubview:starImage];

    NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
      NSString *str=[[NSString alloc]initWithFormat:@"(%@)",boo];
    CGRect labelFrame = CGRectMake(290,18, 40,40);
    UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
    Label.text=str;
    Label.tag=1000;
   // Label.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:Label];

}
 if(n >=3)
{   
    CGRect starFrame = CGRectMake(100,6, 85, 40);

    UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
    starImage.image = [UIImage imageNamed:@"threestar.png"];
    [cell.contentView addSubview:starImage];

    NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
    NSString *str=[[NSString alloc]initWithFormat:@"(%@)",boo];
    CGRect labelFrame = CGRectMake(290,18, 40,40);
    UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
    Label.text=str;
    Label.tag=1000;
   Label.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:Label];

}

  if(n >= 4)
{
    CGRect starFrame = CGRectMake(100,6, 85, 40);

    UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
    starImage.image = [UIImage imageNamed:@"4star.png"];
    [cell.contentView addSubview:starImage];

    NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
     NSString *str=[[NSString alloc]initWithFormat:@"(%@)",boo];
    CGRect labelFrame = CGRectMake(290,18,40,40);
    UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
    Label.text=str;
    Label.tag=1000;
   //Label.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:Label];
}
if(n >= 5)
{
    CGRect starFrame = CGRectMake(100,6, 85, 40);

    UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
    starImage.image = [UIImage imageNamed:@"5star.png"];
    [cell.contentView addSubview:starImage];

    NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
    NSString *str=[[NSString alloc]initWithFormat:@"(%@)",boo];
    CGRect labelFrame = CGRectMake(290,18, 40,40);
    UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
    Label.font = [UIFont systemFontOfSize:18];
    Label.text=str;
    Label.tag=1000;
  // Label.backgroundColor=[UIColor clearColor];
    [cell.contentView addSubview:Label];
}

cell.textLabel.text=[self.story objectAtIndex:indexPath.row];
cell.textLabel.numberOfLines=2;

return cell;

 }

enter image description here

最佳答案

试试这个代码,我已经解决了

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    //[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

    static NSString *CellIdentifier = @"Cell";
    HJManagedImageV* mi;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        mi = [[[HJManagedImageV alloc] initWithFrame:CGRectMake(-18,-2,90,90)] autorelease];
        mi.tag = 999;
        [cell addSubview:mi];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    CGSize sizeTitle = [[self.story objectAtIndex:indexPath.row] sizeWithFont:[UIFont boldSystemFontOfSize:16.0] 
                            constrainedToSize:CGSizeMake(200, 40) 
                                lineBreakMode:UILineBreakModeWordWrap];

    float width = sizeTitle.width;
    NSLog(@"Width %f", width);
    width = width+15;
    NSDictionary *boy=[self.media1 objectAtIndex:indexPath.row];
    NSString *str=[[NSString alloc]initWithFormat:@"%@",boy];
    NSInteger n=[str intValue];
    NSLog(@"the value:%@",str);
    CGRect coreFrame;
    if(width<170){
        coreFrame = CGRectMake(2, 6, width, 40);
    }
    else{
        coreFrame = CGRectMake(2, 6, 170, 40);   
        width = 175;
    }
     float k = width+85;
    UILabel *lab = [[UILabel alloc] initWithFrame:coreFrame];
    lab.text = [self.story objectAtIndex:indexPath.row];
    [cell.contentView addSubview:lab];

    if(n ==0)
    {   
        CGRect starFrame = CGRectMake(width,6, 85, 37);

        UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
        starImage.image = [UIImage imageNamed:@"nostar.png"];
        starImage.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:starImage];

        NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
        NSString *str=[[NSString alloc]initWithFormat:@"(%@)",boo];
       // str=[str substringToIndex:4];
        CGRect labelFrame = CGRectMake(k,6, 40, 40);
        UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];

        Label.text=str;
        Label.tag=1000;
          Label.textColor=[UIColor grayColor];
        [cell.contentView addSubview:Label];


    }
    if(n >=1)
    {   
        CGRect starFrame = CGRectMake(width,6, 85, 37);

        UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
        starImage.image = [UIImage imageNamed:@"1star.png"];
        [cell.contentView addSubview:starImage];

        NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
        CGRect labelFrame = CGRectMake(k,6, 40, 40);
        UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
        NSString *str=[[NSString alloc]initWithFormat:@"(%@)",boo];
        Label.text=str;
        Label.tag=1000;
          Label.textColor=[UIColor grayColor];
        [cell.contentView addSubview:Label];
    }
    if(n >=2)
    {   
        CGRect starFrame = CGRectMake(width,6, 85, 37);

        UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
        starImage.image = [UIImage imageNamed:@"twostar.png"];
        [cell.contentView addSubview:starImage];

        NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
          NSString *str=[[NSString alloc]initWithFormat:@"(%@)",boo];
        CGRect labelFrame = CGRectMake(k,6, 40,40);
        UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
               Label.text=str;
        Label.tag=1000;
          Label.textColor=[UIColor grayColor];
       // Label.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:Label];
    }
     if(n >=3)
    {   
        CGRect starFrame = CGRectMake(width,6, 85, 37);

        UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
        starImage.image = [UIImage imageNamed:@"threestar.png"];
        [cell.contentView addSubview:starImage];

        NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
        NSString *str=[[NSString alloc]initWithFormat:@"(%@)",boo];
      //  str=[str substringToIndex:4];

        CGRect labelFrame = CGRectMake(k,6, 40,40);
        UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];

        Label.text=str;
        Label.tag=1000;
          Label.textColor=[UIColor grayColor];
            [cell.contentView addSubview:Label];

    }
   if(n >= 4)
    {
        CGRect starFrame = CGRectMake(width,6, 85, 37);

        UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
        starImage.image = [UIImage imageNamed:@"4star.png"];
        [cell.contentView addSubview:starImage];

        NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
         NSString *str=[[NSString alloc]initWithFormat:@"(%@)",boo];
        //str=[str substringToIndex:4];

        CGRect labelFrame = CGRectMake(k,6,40,40);
        UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];

        Label.text=str;
        Label.tag=1000;
        Label.textColor=[UIColor grayColor];
       //Label.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:Label];
    }
    if(n >= 5)
    {
        CGRect starFrame = CGRectMake(width,6, 85, 37);

        UIImageView *starImage = [[[UIImageView alloc] initWithFrame:starFrame] autorelease];
        starImage.image = [UIImage imageNamed:@"5star.png"];
        [cell.contentView addSubview:starImage];

        NSString *boo=[[NSString alloc]initWithFormat:@"%d",n];
        NSString *str=[[NSString alloc]initWithFormat:@"(%@)",boo];
       // str=[str substringToIndex:4];

        CGRect labelFrame = CGRectMake(k,6, 40,40);
        UILabel *Label = [[[UILabel alloc] initWithFrame:labelFrame] autorelease];
        Label.font = [UIFont systemFontOfSize:18];

        Label.text=str;
        Label.tag=1000;
        Label.textColor=[UIColor grayColor];
      // Label.backgroundColor=[UIColor clearColor];
        [cell.contentView addSubview:Label];
    }
    return cell;
}

关于iphone - 如何将星级图像放置在文本标签旁边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9260155/

相关文章:

java - 体验 Vaadin touchkit

iphone - Authorize.net ios Sdk 错误

ios - Xcode 8 中的位码错误,但仅适用于模拟器

ios - 仅将 subview 添加到当前 View 的 UINavigationBar

android - 如何识别设备 token 是用于 Android 还是 iOS?

iphone - 删除 NSString ios 中标记 <a> 的每次出现

iphone - JSON 提取到 iOS 应用程序

ios - 将日期和时间转换为时间戳

ios - 是否有充分的理由反对让我的 TyphoonAssembly 成为单例?如果是这样,为什么?如果没有,是否有推荐的方法?

iphone - iOS 应用程序上的 OpenFeint 集成