2

我正在制作一个应用程序,其中有 MKMapView 和一些 MKAnnotations。我还有一个列表(一个 UITableView),其中包含有关由这些注释表示的位置的更多信息,其中我的问题发生在其中。我使用我自己的 UITableViewCell 子类,它有 3 个 UILabel:地点名称的默认 textLabel、addressLabel 和 phoneNumberLabel。我必须考虑这些标签中的文本太大而无法在一行中显示的情况,并相应地调整 UILabels 的大小。我还必须考虑没有电话号码或地址并且 UILabel 应该“消失”(帧大小为 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 的一部分,它既是我的 2 个表的委托又是数据源 - categoryTable 工作得很好,它是 poiTable 我有问题。

poiArraysDictionary 是一个字典,其中键是兴趣点类别的名称,值是该类别的点数组。点本身是我从服务器获取的 JSON 格式的字典,带有名称、地址(可以为空)和电话(可以为空)等键。

- (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;
    }
}
4

0 回答 0