Web サービスにいくつかの製品があります。そこから製品名を取得し、ラベル テキストを使用してテーブルビューに表示しています。そして、私はそれを手に入れました。私の問題は、テーブルビューで製品を選択すると、didselectrowatindexpath が空の場合でも、ラベルが再ロードされ、既存のラベル テキストで上書きされることです。これを使った、
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}
bookName = [[UILabel alloc]initWithFrame:CGRectMake(100, 3, 180, 25)];
NSString *text = [NSString stringWithFormat:@"%@",[[stories objectAtIndex:indexPath.row] objectForKey: @"product_name"]];
bookName.text = text;
bookName.textAlignment = UITextAlignmentCenter;
bookName.lineBreakMode = UILineBreakModeWordWrap;
[bookName setTextColor:[UIColor blackColor]];
CGSize expectedLabelSize = [text sizeWithFont:bookName.font constrainedToSize:bookName.frame.size lineBreakMode:UILineBreakModeWordWrap];
CGRect newFrame = bookName.frame;
newFrame.size.height = expectedLabelSize.height;
bookName.frame = newFrame;
bookName.numberOfLines = 0;
[bookName sizeToFit];
[cell.contentView addSubview:bookName];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}