2012-02-13 51 views
0

即時解析json提要,並根據解析的值將im放置在表視圖單元格中的星圖像。我想要的是儘快放置星級評分圖像文本標籤得到完成星級評價圖像變得靈活,沒有固定的框架。下面是輸出結果的代碼和屏幕截圖。如何在文本標籤旁邊放置星級評分圖片

- (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

回答

1

嘗試這個代碼,我已經解決了

- (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; 
} 
0

可以使用下面的方法來計算串的寬度和高度 -

- sizeWithFont:
- sizeWithFont:forWidth:lineBreakMode:
- sizeWithFont: constrainedToSize:
- sizeWithFont:constrainedToSize:lineBreakMode:
- sizeWithFont:minFontSize:actualFontSize:forWidth:lineBr eakMode:

,或者你可以把你需要設置標籤寬度寬度的基礎上,在這裏https://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html

的文檔,然後設置你的明星圖像的幀。

一個例子 -

CGSize sizeTitle = [titleStr sizeWithFont:[UIFont boldSystemFontOfSize:16.0] 
                                      constrainedToSize:CGSizeMake(200, 40) 
                                          lineBreakMode:UILineBreakModeWordWrap]; 

float width = sizeTitle.width; 
+0

ü可以給我根據上面的代碼中的小例子來放置images..will不勝感激 – kingston 2012-02-13 12:09:03

+0

考慮更新條件如果(n <= 5)我應該刪除CGRect starFrame – kingston 2012-02-13 12:21:29

+0

可以添加兩行以上的寬度添加圖像.. ima noob – kingston 2012-02-13 12:21:58