我正在制作一个应用程序,其中我有MKMapView并带有一些MKAnnotations。我还有一个列表(一个UITableView),其中包含有关这些注释所代表的地方的更多信息,我的问题就出现了。我使用自己的UITableViewCell子类,它有3个UILabel:地方名称的默认textLabel,addressLabel和phoneNumberLabel。我必须考虑这些标签中的文本太大而无法在一行中显示的情况,并相应地调整UILabel的大小。我还必须考虑没有电话号码或地址的情况,根据UILabels应该"消失" (帧大小为0)。
问题是,当我向下滚动表格时,单元格从屏幕的上半部分消失(我进一步滚动,它们中的更多是不可见的,并且对于大多数UITableView,根本没有显示单元格,只是节标题,即使整个表的大小没有变化)。
(截图应该在这里,但stackoverflow不允许我发布它:() ![在此输入图像说明] [1]
这是我的自定义UITableViewCell子类:
@implementation POICell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.addressLabel = [[UILabel alloc] init];
self.phoneNumberLabel = [[UILabel alloc] init];
[self addSubview:self.addressLabel];
[self addSubview:self.phoneNumberLabel];
self.addressLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
self.phoneNumberLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
self.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
self.addressLabel.lineBreakMode = UILineBreakModeWordWrap;
self.phoneNumberLabel.lineBreakMode = UILineBreakModeWordWrap;
self.textLabel.lineBreakMode = UILineBreakModeWordWrap;
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect titleFrame = self.frame;
titleFrame.origin.x += 30;
titleFrame.size.width -= 60;
titleFrame.size.height = [self.textLabel.text sizeWithFont:self.textLabel.font forWidth:self.textLabel.frame.size.width lineBreakMode:NSLineBreakByWordWrapping].height;
self.textLabel.frame = titleFrame;
if (self.addressLabel.text != nil) {
self.addressLabel.frame = CGRectMake(titleFrame.origin.x, [DDUtility yRelativeTo:self.textLabel withMargin:0], titleFrame.size.width, [self.addressLabel.text sizeWithFont:self.addressLabel.font forWidth:self.addressLabel.frame.size.width lineBreakMode:NSLineBreakByWordWrapping].height);
}
else {
self.addressLabel.frame = CGRectMake(titleFrame.origin.x, [DDUtility yRelativeTo:self.textLabel withMargin:0], 0, 0);
}
NSString *labelText = self.addressLabel.text;
if (self.phoneNumberLabel.text != nil) {
self.phoneNumberLabel.frame = CGRectMake(titleFrame.origin.x, [DDUtility yRelativeTo:self.addressLabel withMargin:0], titleFrame.size.width, [self.phoneNumberLabel.text sizeWithFont:self.phoneNumberLabel.font forWidth:self.phoneNumberLabel.frame.size.width lineBreakMode:NSLineBreakByWordWrapping].height);
}
else {
self.phoneNumberLabel.frame = CGRectMake(titleFrame.origin.x, [DDUtility yRelativeTo:self.addressLabel withMargin:0], 0, 0);
}
labelText = self.phoneNumberLabel.text;
}
这里是我的UIViewController的一部分,它是我的两个表的委托和数据源 - categoryTable工作得很好,它有点问题。
poiArraysDictionary是一个字典,其中键是Point of Interest类别的名称,值是此类别的点数组。点本身是我从服务器获取的字典,包括名称,地址(可以为空)和电话(可以为空)等。
- (void) viewDidLoad {
(...)
UITableView *poiTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, listView.frame.size.width, listView.frame.size.height - 120) style:UITableViewStylePlain];
poiTable.separatorStyle = UITableViewCellSeparatorStyleNone;
//poiTable.rowHeight = 80;
poiTable.dataSource = self;
poiTable.delegate = self;
poiTable.backgroundColor = [UIColor clearColor];
self.poiTable = poiTable;
[poiTable reloadData];
[self.listView addSubview:poiTable];
(...)
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView == self.categoryTable) {
return 1;
}
else {
return [self.categoryArray count];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.categoryTable) {
return [self.categoryArray count];
}
else {
return [[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:section]] count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == self.categoryTable) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"categoryCell"];
if (cell == nil) {
cell = [[[CategoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"categoryCell"] autorelease];
}
cell.textLabel.text = [self.categoryArray objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
return cell;
}
else {
POICell *cell = [tableView dequeueReusableCellWithIdentifier:@"poiCell"];
if (cell == nil) {
cell = [[[POICell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"poiCell"] autorelease];
}
/*int maxIndex = [self.pointsOfInterestArray count];
int index;
int indexInSection = 0;
for (index = 0; index < maxIndex; ++index) {
if ([((NSString*)([((NSDictionary*)([self.pointsOfInterestArray objectAtIndex:index])) objectForKey:@"category"])) isEqualToString:[self.categoryArray objectAtIndex:indexPath.section]]) {
if (indexInSection != indexPath.row) {
++indexInSection;
}
else {
break;
}
}
}
cell.textLabel.text = [[self.pointsOfInterestArray objectAtIndex:index] objectForKey:@"name"];
if ([[[self.pointsOfInterestArray objectAtIndex:index] objectForKey:@"address"] class] != [NSNull class]) {
cell.addressLabel.text = [[self.pointsOfInterestArray objectAtIndex:index] objectForKey:@"address"];
}
if ([[[self.pointsOfInterestArray objectAtIndex:index] objectForKey:@"phone"] class] != [NSNull class]) {
cell.phoneNumberLabel.text = [[self.pointsOfInterestArray objectAtIndex:index] objectForKey:@"phone"];
}*/
if ([[[[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] objectForKey:@"name"] class] == [NSNull class]) {
return nil;
}
cell.textLabel.text = [[[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] objectForKey:@"name"];
if ([[[[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] objectForKey:@"address"] class] == [NSNull class]) {
cell.addressLabel.text = nil;
}
else {
cell.addressLabel.text = [[[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] objectForKey:@"address"];
}
if ([[[[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] objectForKey:@"phone"] class] == [NSNull class]) {
cell.phoneNumberLabel.text = nil;
}
else {
cell.phoneNumberLabel.text = [@"telefon: " stringByAppendingString:[[[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row] objectForKey:@"phone"]];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.backgroundColor = [UIColor clearColor];
CGRect cellFrame = cell.frame;
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (tableView == self.categoryTable) {
return 0;
}
else {
return 40;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.categoryTable) {
return 50;
}
else {
NSDictionary *point = [[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
CGFloat rowHeight = 0;
if ([[point objectForKey:@"name"] class] != [NSNull class]) {
rowHeight += [[point objectForKey:@"name"] sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:20] forWidth:self.poiTable.frame.size.width lineBreakMode:NSLineBreakByWordWrapping].height;
}
if ([[point objectForKey:@"address"] class] != [NSNull class]) {
rowHeight += [[point objectForKey:@"address"] sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14] forWidth:self.poiTable.frame.size.width lineBreakMode:NSLineBreakByWordWrapping].height;
}
if ([[point objectForKey:@"phone"] class] != [NSNull class]) {
rowHeight += [[point objectForKey:@"phone"] sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14] forWidth:self.poiTable.frame.size.width lineBreakMode:NSLineBreakByWordWrapping].height;
}
return rowHeight;
}
}