即時解析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;
}
ü可以給我根據上面的代碼中的小例子來放置images..will不勝感激 – kingston 2012-02-13 12:09:03
考慮更新條件如果(n <= 5)我應該刪除CGRect starFrame – kingston 2012-02-13 12:21:29
可以添加兩行以上的寬度添加圖像.. ima noob – kingston 2012-02-13 12:21:58