我想在我的UITableView自定义Cell中创建一个动态高度标签,为此我写了一个代码lile为
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifierTwo=@"CellThree";
LargeTableViewCell *cellThree=[tableView dequeueReusableCellWithIdentifier:CellIdentifierTwo];
if (cellThree == nil)
{
NSArray *nibTwo=[[NSBundle mainBundle]loadNibNamed:@"LargeTableViewCell" owner:self options:nil];
cellThree=[nibTwo objectAtIndex:0];
}
{
cellThree.selectionStyle=UITableViewCellSelectionStyleNone;
NSString *str = [[self.jsonDictionary valueForKey:@"review"]objectAtIndex:0];
CGRect frame = [str boundingRectWithSize:CGSizeMake(288, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:nil
context:nil];
cellThree.largeTextView.frame = frame;
cellThree.contentView.layer.cornerRadius=4.0f;
if (![self.jsonDictionary isKindOfClass:[NSNull class]])
{
cellThree.largeTextView.text=[[self.jsonDictionary valueForKey:@"review"]objectAtIndex:0];
}
}
[cellThree layoutSubviews];
return cellThree;
}
和tableview heightForRowAtIndexPath
方法如
NSString *str = [[self.jsonDictionary valueForKey:@"review"]objectAtIndex:0];
CGRect frame = [str boundingRectWithSize:CGSizeMake(288, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:nil
context:nil];
return frame.size.height+50;
然后我得到了我想要的tableview单元格高度,但是我的表格视图单元格不包含我之后仅包含的所有解析字符串请给我解决方案。 这里我的JsonDictionary输出代码如as
<p>The potential of a geographically mobile spirit is vast. When Ravi Babu (playing the spirit) made a surreptitious visit to the police station eavesdropping on Mohinis (Poorna) complaint, comfortably seated next to her, it was clear that the spirit was here to say as its freedom of movement cant be restricted by whatever means. In Avunu-2, it (or, should we say he because the spirit is a rapist?) arrives on the scene using the elevator and enters the new, upscale apartment through the main door. </p><p>The migration-capable rapist-spirit is also tech-savvy and nothing can stop him except an amulet made in Varanasi. The characters at the receiving end, too, are the same, with an addition made in the form of Harshas friend. After being scared in the old, relatively low-budget flat in Avunu-1, the young couple are all gung-ho about nocturnal as well as day-time romance alike (in one scene, a lip-lock is hindered by an unromantic glass pane), having seemingly forgotten all about their recent near-death experience. So much so, when Mohini says Raju mallee vachadu, Harsha shouts back, saying Clear ga chepu. What? Seriously? </p><p>In the first part we saw how a newly married couple was prevented by the spirit of Raju from consummating their marriage. He wants to possess the good husband and in his lecherous mission, he was aided by a foolhardy husband who forced his wife to stay in the flat despite she being devastated and extremely frightened. That was Part-I. Here you have a camera-woman who can capture a spirit and presumably even change the world asking Mohini to do the unthinkable. </p><p>Part-II is a sorry repetition of the first part, but the novelty of the concept is badly over-used to the point of looking jaded. There is the same leering away at a nude Mohini while she is bathing, there is the same pulling off the rug while the couple is sleeping, there are the same attempts to undress her (this time by the good husband, too), there is the same technology-enabled hint that a third entity is roaming about. </p><p>Whereas in the first part Gods power could not defeat the evil, here it succeeds for good part. The evil spirit may well leave the world exactly after one year, not a single minute delayed. Or, so we think. It strikes back, in another locality, at Prabhas Sreenus curses. </p><p>Screenplay-wise, this is definitely not in the same league as Ravi Babus other films. While presenting the new element of a drunkard neighbor who with his bizarre tantrums and loving affection towards his wife (he says bangaram to her, at all times) presents a dichotomous picture, Ravi Babu scores on the concept but surely fails on the treatment . One waits for something interesting about his story and the climax doesnt disappoint. The couple is played by Nikita and a well-known character artiste. </p><p>Just to make sure Sanjjanas incredible camera is established in the audiences mind, Ravi Babu goes for a scene that borders on the stupid, both idea-wise and execution-wise. </p><p>The hi-tech apartment is a visual treat for sure. The scene where Harshas friend explains the features like a salesman could have been avoided. </p><p>Ravi Babu banks on some voyeuristic moments here and there. There is no intelligence in story-telling and enlisting a spiritual camera cant be called innovative. If that is his idea of coming up with something akin to what Villa-2 or the recent Pisachi had, then it is disappointing. </p><p>In a sequel like this, having the characters behave more intelligently makes all the difference. What do Hasha and Mohini do with their hands-on experience of dealing with the spirit? </p><p>The performances are on the same lines. Poorna looks less glamorous and Harsha is ok. </p><p>Verdict: Sans intelligent story-telling, Avunu-2 entertains only in bits and pieces. Good RR and cinematography notwithstanding, the film is more of the same. Even at 107 minutes, the film woks only in bits and pieces. </p><p>Rating: 2.25/5 </p>
我的标签包含的值如
<p>The potential of a geographically mobile spirit is vast. When Ravi Babu (playing the spirit) made a surreptitious visit to the police station eavesdropping on Mohinis (Poorna) complaint, comfortably seated next to her, it was clear that the spirit was here to say as its freedom of movement cant be restricted by whatever means. In Avunu-2, it (or, should we say he because the spirit is a rapist?) arrives on the scene using the elevator and enters the new, upscale apartment through the main door. </p><p>The migration-capable rapist-spirit is also tech-savvy and nothing can stop him except an amulet made in Varanasi. The characters at the receiving end, too, are the same, with an addition made in the form of Harshas friend. After being scared in the old, relatively low-budget flat in Avunu-1, the young couple are all gung-ho about nocturnal as well as day-time romance alike (in one scene, a lip-lock is hindered by an unromantic glass pane), having seemingly forgotten all about their recent near-death experience. So much so, when Mohini says Raju mallee vachadu, Harsha shouts back, saying Clear ga chepu. What? Seriously? </p><p>In the first part we saw how a newly married couple was prevented by the spirit of Raju from consummating their marriage. He wants to possess the good husband and in his lecherous mission, he was aided by a foolhardy husband who forced his wife to stay in the flat despite she being devastated and extremely frightened. That was Part-I. Here you have a camera-woman who can capture a spirit and presumably even change the world asking Mohini to do the unthinkable. </p><p>Part-II is a sorry repetition of the first part, but the novelty of the concept is badly over-used to the point of looking jaded. There is the same leering away at a nude Mohini while she is bathing, there is the same pulling off the rug while the couple is sleeping, there are the same attempts to undress her (this time by the good husband, too), there is the same technology-enabled hint that a third entity is roaming about. </p><p>Whereas in the first part Gods power could not defeat the evil, here it succeeds for good part. The evil spirit may well leave the world exactly after one year, not a single minute delayed. Or, so we think. It strikes back, in another locality, at Prabhas Sreenus curses. </p><p>Screenplay-wise, this is definitely not in the same league as Ravi Babus other films. While presenting the
答案 0 :(得分:0)
尝试使用此代码:
+ (CGFloat)heightForText:(NSString *)text labelWidth:(float)labelWidth font:(UIFont *)font
{
if(!text || labelWidth == 0 || !font)
return 0.0f;
NSDictionary *attributes = @{NSFontAttributeName:font};
CGSize size = CGSizeMake(labelWidth, 9999);
CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
CGSize textSize = rect.size;
return textSize.height;
}
使用上述方法使用-tableview:heightForRowAtindexPath方法更改行高。
答案 1 :(得分:0)
尝试此解决方案:
第1步:使用此方法计算标签高度取决于您的文字。
- (CGFloat)heightForText:(NSString *)text labelWidth:(float)labelWidth font:(UIFont *)font
{
CGSize size = CGSizeMake(labelWidth, 9999);
NSDictionary *attributes = @{NSFontAttributeName:font};
CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
float textHeight = rect.size.height;
return textHeight;
}
第2步:设置表格单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifierTwo=@"CellThree";
LargeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifierTwo];
NSString *txt = [[self.jsonDictionary valueForKey:@"review"]objectAtIndex:indexPath.row];
return cellheight + [self heightForText:txt labelWidth:cell.largeTextView.frame.size.width font:largeTextView.font];
}
答案 2 :(得分:0)
试试这段代码:
- (CGSize)heightForText:(NSString *)text Width:(float)expectedWidth Font:(UIFont *)font
{
CGSize size = CGSizeMake(labelWidth, 9999);
UITextView *text = [[UITextView alloc]init];
[text setFont:font];
[text setText:str];
return [text sizeThatFits:size];
}
并在heightForRowAtIndex
中调用它 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *txt = [[self.jsonDictionary valueForKey:@"review"]objectAtIndex:indexPath.row];
return cellheight + [self heightForText:txt Width:cell.largeTextView.frame.size.width Font:(UIFont *)largeTextView.font];
}