即时解析json feed并根据解析后的值我将星形图像放置在表格视图单元格中。我想要的是在文本标签中的单词没有完成后立即放置星级图像星级评分图像到达灵活,没有固定的框架。下面是输出结果的代码和截图。
- (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;
}
答案 0 :(得分: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;
}
答案 1 :(得分:0)
您可以使用以下方法计算字符串的宽度和高度 -
- sizeWithFont:
- sizeWithFont:forWidth:lineBreakMode:
- sizeWithFont:constrainedToSize:
- sizeWithFont:constrainedToSize:lineBreakMode:
- sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:
根据宽度,您需要设置标签宽度,然后设置星形图像的边框。
一个例子 -
CGSize sizeTitle = [titleStr sizeWithFont:[UIFont boldSystemFontOfSize:16.0]
constrainedToSize:CGSizeMake(200, 40)
lineBreakMode:UILineBreakModeWordWrap];
float width = sizeTitle.width;