Ios 自定义UITableViewCells在滚动时消失
我正在制作一个应用程序,其中我有一些MKMapView注释。我还有一个列表(UITableView),其中包含了关于这些注释所表示的位置的更多信息,我的问题就发生在这些位置。我使用自己的UITableViewCell子类,它有3个UILabel:默认的textLabel,用于地名、addressLabel和phoneNumberLabel。我必须考虑这些标签中的文本太大,不能在一行中显示,并相应地调整uLabeles的情况。我也要考虑的情况下,没有电话号码或地址,根据UIABABELS应该“消失”(帧大小为0)。 问题是,当我向下滚动表格时,单元格会从屏幕的上部消失(我滚动得越远,其中就有越多的单元格不可见,而对于大多数UITableView来说,根本没有显示单元格,只有部分标题,即使整张表格的大小没有改变) (截图应该放在这里,但stackoverflow不允许我发布:() ![在此处输入图像描述][1] 以下是我的自定义UITableViewCell子类:Ios 自定义UITableViewCells在滚动时消失,ios,ipad,uikit,Ios,Ipad,Uikit,我正在制作一个应用程序,其中我有一些MKMapView注释。我还有一个列表(UITableView),其中包含了关于这些注释所表示的位置的更多信息,我的问题就发生在这些位置。我使用自己的UITableViewCell子类,它有3个UILabel:默认的textLabel,用于地名、addressLabel和phoneNumberLabel。我必须考虑这些标签中的文本太大,不能在一行中显示,并相应地调整uLabeles的情况。我也要考虑的情况下,没有电话号码或地址,根据UIABABELS应该“消失
@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是一个字典,其中键是兴趣点类别的名称,值是该类别点的数组。点本身是我作为JSON从服务器上获取的字典,具有像name、address(可以为null)和phone(可以为null)这样的键
-(void)viewDidLoad{
(...)
UITableView*poiTable=[[UITableView alloc]initWithFrame:CGRectMake(0,120,listView.frame.size.width,listView.frame.size.height-120)样式: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)表格视图中的节数:(UITableView*)表格视图{
if(tableView==self.categoryTable){
返回1;
}
否则{
返回[self.categoryArray count];
}
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
if(tableView==self.categoryTable){
返回[self.categoryArray count];
}
否则{
返回[[self.poiArraysDictionary objectForKey:[self.categoryArray objectAtIndex:section]]count];
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
if(tableView==self.categoryTable){
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“categoryCell”];
如果(单元格==nil){
cell=[[CategoryCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“CategoryCell”]自动释放];
}
cell.textlab.text=[self.categoryArray objectAtIndex:indexPath.row];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.backgroundColor=[UIColor clearColor];
返回单元;
}
否则{
POICell*单元格=[tableView dequeueReusableCellWithIdentifier:@“POICell”];
如果(单元格==nil){
cell=[[POICell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“POICell”]自动释放];
}
/*int maxIndex=[self.pointsOfInterestArray计数];
整数指数;
int indexInSection=0;
对于(索引=0;索引- (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;
}
}