我在表视图中显示数据库中的数据。在表格视图中,单元格包含根据数据库值的动态高度标签。假设我想在表格视图中用数据库中的数据库显示任何城市的描述,我在数据库中手动添加了描述。我的问题是某些时间标签没有显示完整描述,因为我的手机和sqlite数据库的320 px的标签宽度超过320.在这种情况下,所有格式都在iphone中受到干扰。我必须更改数据库中的间距或标签然后在iphone中测试。我有很多条目,这是非常耗时的。如果我在数据库中给出大量的间距,那么Label将在描述完成后显示空白。 有没有办法解决这个格式化问题。
这是我的代码
- (UITableViewCell *)tableView:(UITableView *)atableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//label to hold description of city
UILabel *label = nil;
//cell identifier for each section oftable
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [atableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:@"Cell"]autorelease];
//label initialization
label = [[UILabel alloc]initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:kFontSize];
[label setNumberOfLines:0];
[label setFont:[UIFont fontWithName:@"Futura" size:13.0]];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[label setTag:1];
//adding label to cell's content view
[[cell contentView]addSubview:label];
[label release];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// text to display in label in cell
NSString *text = city.Description;
CGSize constraint = CGSizeMake(kCellContentWidth - (kCellContentMargin * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:kFontSize]
constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(kCellContentMargin,
kCellContentMargin,
kCellContentWidth - (kCellContentMargin *4),
MAX(size.height, 44.0f))];
return cell;
}
//Dynamic size label according to description
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text = city.Description;
CGSize constraint = CGSizeMake(kCellContentWidth - (kCellContentMargin *2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:kFontSize] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height , 44.0f);
return height + (kCellContentMargin *2); //returning height of label with font size and constaraint size
}
我的城市模型课程是
@interface City : NSManagedObject
{
}
@property (nonatomic, retain) NSString * Description;
@property (nonatomic, retain) NSString * Name;
@property (nonatomic, retain) NSData * Image;
@property (nonatomic, retain) NSSet* cityToRestaurant;
@property (nonatomic, retain) NSSet* cityToHotel;
@property (nonatomic, retain) NSSet* cityToPlaces;
@end
@interface City (CoreDataGeneratedAccessors)
- (void)addCityToRestaurantObject:(NSManagedObject *)value;
- (void)removeCityToRestaurantObject:(NSManagedObject *)value;
- (void)addCityToRestaurant:(NSSet *)value;
- (void)removeCityToRestaurant:(NSSet *)value;
- (void)addCityToHotelObject:(NSManagedObject *)value;
- (void)removeCityToHotelObject:(NSManagedObject *)value;
- (void)addCityToHotel:(NSSet *)value;
- (void)removeCityToHotel:(NSSet *)value;
- (void)addCityToPlacesObject:(NSManagedObject *)value;
- (void)removeCityToPlacesObject:(NSManagedObject *)value;
- (void)addCityToPlaces:(NSSet *)value;
- (void)removeCityToPlaces:(NSSet *)value;
和实施文件
#import "City.h"
@implementation City
@dynamic Description;
@dynamic Name;
@dynamic Image;
@dynamic cityToRestaurant;
@dynamic cityToHotel;
@dynamic cityToPlaces;
@end