在iPhone TableView上的某些行上不显示DetailTextLabel

时间:2012-02-27 11:19:22

标签: iphone ios uitableview

由于某种原因,某些细节标签不会显示。我已调试程序,我检查过,细节标签文本是正常的,在这种情况下,它是一个距离。我剪切了文本标签文本,因此它不会与细节标签重叠。另外,我没有得到距离没有填补!我的代码中的消息。这是截图(查看第2行和第4行):

enter image description here

这是该视图控制器的完整代码:

    #import "LpuListTableViewController.h"
#import "MedicineAppDelegate.h"
#import "ResultLPU.h"
#import "DetailViewController.h"
#import "MapViewController.h"
#import "CustomCellBackgroundView.h"
#import "CustomCellView.h"
#import "CSCustomCellBackgroundView.h"

#define allTrim( object ) [object stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet] ]

@implementation LpuListTableViewController

@synthesize myTableView;
@synthesize tableData;
@synthesize lpuArray,lpuArray1,lpuArray3,lpuArray6;

- (void) showAllOnMap {
    useDelegate

    appDelegate.singleLPUModeOn = NO;

    MapViewController *mapViewController = [[MapViewController alloc] initWithNibName:@"MapViewController" bundle:nil];

    [self.navigationController pushViewController:mapViewController animated:YES];

    [mapViewController release];
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"mapWhite.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(showAllOnMap)];

    // Если нет интернета, игнорируем

    useDelegate
    if (!appDelegate.internetActive) {
        NSString *msg = NoInternetMsg;
        [appDelegate showResultViewWithText:msg fromView:self.view withSpeed:ResultViewSpeed];
        return;
    }

    CGRect frame = CGRectMake(30.0, 40.0, 260.0, 150.0);
    waitView = [[UIView alloc] initWithFrame:frame];
    waitView.backgroundColor = AlertViewColor;
    waitView.layer.cornerRadius = 15;

    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    // Универсальное определение центра iPhone / iPad
    CGFloat centerX = screenSize.width/2.0;
    CGFloat centerY = (screenSize.height - 20.0 - 44.0 - 50.0)/2.0;
    waitView.center = CGPointMake(centerX, centerY);

    UILabel *aLabel = [[UILabel alloc]initWithFrame:CGRectMake(5.0,20.0,260.0,40.0)];
    aLabel.backgroundColor = [UIColor clearColor];
    aLabel.text = AlertViewText;
    aLabel.textAlignment = UITextAlignmentCenter;
    aLabel.font = [UIFont fontWithName:MyFont size:AlertViewTextSize];
    [waitView addSubview:aLabel];

    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    indicator.center = CGPointMake(waitView.bounds.size.width * 0.5f, waitView.bounds.size.height * 0.7f);
    [indicator startAnimating];
    [waitView addSubview:indicator];
    [indicator release];

    [self.view addSubview:waitView];

    [self performSelectorInBackground:@selector(loadDataInBackground) withObject:nil];




    //NSLog(@"Count of lpuArray objects:%d", [lpuArray count]);

    //tableData = [[NSMutableArray alloc]initWithArray:lpuArray];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

- (void) loadDataInBackground {

    useDelegate

    // Set up a pool for the background task.
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    @try {
        // Заполняем ЛПУ
        int maxDist = APIMaximumDistanceToLPU;
        NSLog(@"Selected Lpu Type:%@", appDelegate.selectedLpuType);

        lpuArray = [[NSMutableArray alloc] init];
        lpuArray1 = [[NSMutableArray alloc] init];
        lpuArray3 = [[NSMutableArray alloc] init];
        lpuArray6 = [[NSMutableArray alloc] init];

        lpuArray = [appDelegate retrieveLPUofType:[appDelegate.selectedLpuType intValue]withMaxDist:maxDist];

        // Разделяем lpuArray на несколько массивов в зависимости от того, какое расстояние там

        lpuArray1 = [appDelegate selectFromLpuArray:lpuArray withMinDist:0 withMaxDist:1000];
        lpuArray3 = [appDelegate selectFromLpuArray:lpuArray withMinDist:1000 withMaxDist:3000];
        lpuArray6 = [appDelegate selectFromLpuArray:lpuArray withMinDist:3000 withMaxDist:7000];

        [NSThread sleepForTimeInterval:testSleepInterval];
        [self performSelectorOnMainThread:@selector(completeLoad) withObject:nil waitUntilDone:YES];
    }
    @finally {
        [pool drain];
    }
}


- (void) completeLoad {    
    [waitView removeFromSuperview];
    [waitView release];
    waitView = nil;
    [myTableView reloadData];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    useDelegate

    //[tableData removeAllObjects];

    //[tableData addObjectsFromArray:lpuArray];

    [myTableView reloadData];

    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    switch (section) {
        case 0:
            return [lpuArray1 count];
            break;
        case 1:
            return [lpuArray3 count];
            break;
        case 2:
            return [lpuArray6 count];
            break;          
        default:
            return 0;
            break;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    switch (section) {
        case 0:
        {
            if ([lpuArray1 count]>0)
                return @"Не дальше 1 км";
            else 
                return @"";
            break;
        }
        case 1:
        {
            if ([lpuArray3 count]>0)
                return @"Не дальше 3 км";
            else 
                return @"";
            break;
        }
        case 2:
        {
            if ([lpuArray6 count]>0)
                return @"Не дальше 7 км";
            else 
                return @"";
            break;
        }
        default:
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    useDelegate

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.textLabel.font = [UIFont fontWithName:MyFont size:MySize];
        cell.detailTextLabel.font = [UIFont fontWithName:MyFont size:MySize];

        /*cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.detailTextLabel.backgroundColor = [UIColor clearColor];

        cell.contentView.backgroundColor = BigCellColor;*/

    }

    // Configure the cell...
    NSUInteger row = [indexPath row];
    NSUInteger section = [indexPath section];

    // Configure the cell...

    ResultLPU *resultLPU;

    switch (section) {
        case 0:
        {
            resultLPU = [lpuArray1 objectAtIndex:row];
            break;
        }
        case 1:
        {
            resultLPU = [lpuArray3 objectAtIndex:row];
            break;
        }
        case 2:
        {
            resultLPU = [lpuArray6 objectAtIndex:row];
            break;
        }
        default:
            break;
    }

    // Определение расстояния от пользователя до ЛПУ

    float distanceMeters = [resultLPU distanceToLPU];  
    NSString *dist;

    if (distanceMeters < 7000000.0) {
        dist = [NSString stringWithFormat:@"%.0f м", distanceMeters];
    }
    else
    {
        dist = @"";
    }

    //NSString *nameAndDist = [resultLPU.name stringByAppendingString:dist];

    //NSLog(@"dist:%@", dist);
    //NSLog(@"nameAndDist:%@", nameAndDist);

    if ([resultLPU.name length] >= MaxLPUCellTextLength) {
                cell.textLabel.text = [NSString stringWithFormat:@"%@ ...", [resultLPU.name substringToIndex:MaxLPUCellTextLength]];
    }

    else
        cell.textLabel.text = resultLPU.name;

    cell.detailTextLabel.text = dist;
    if ([cell.detailTextLabel.text isEqualToString:@""]) {
        //NSLog(@"Distance is not filled! Dist = %@", dist);
        NSLog(@"Distance is not filled! Dist = %f", distanceMeters);
    }
    else {
        NSLog(@"Distance = %@", dist);
    }


    return cell;
}

/*- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return MyCellHeight;
}*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    useDelegate

    NSUInteger row = [indexPath row];
    NSUInteger section = [indexPath section];
    ResultLPU *resultLPU;

    switch (section) {
        case 0:
            resultLPU = [lpuArray1 objectAtIndex:row];
            break;
        case 1:
            resultLPU = [lpuArray3 objectAtIndex:row];
            break;
        case 2:
            resultLPU = [lpuArray6 objectAtIndex:row];
            break;
        default:
            break;
    }

    appDelegate.selectedLPU = resultLPU;

    // Формируем Description для данного ЛПУ
    appDelegate.LPUdetails = [resultLPU generateHTML];
    appDelegate.selectedItem = resultLPU.name;
    appDelegate.latitudeLPU = [resultLPU.latitude doubleValue];
    appDelegate.longitudeLPU = [resultLPU.longitude doubleValue];
    appDelegate.detailsMode = 2;
    appDelegate.favMode = NO;

    /*appDelegate.latitudeLPU = 55.75;
    appDelegate.longitudeLPU = 37.62;*/

    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];

    [self.navigationController pushViewController:detailViewController animated:YES];

    [detailViewController release];
}

@end

我试过使用StyleSubtitle:它有效。但是我想,为什么StyleValue1没有呢?

enter image description here

3 个答案:

答案 0 :(得分:1)

尝试使用UITableViewCellStyleSubtitle

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

希望这有帮助

答案 1 :(得分:1)

也许是因为这些行的距离计是&gt; = 7000000.0。

更改

NSLog(@"Distance is not filled! Dist = %@", dist);

NSLog(@"Distance is not filled! Dist = %f", distanceMeters);

检查输出。

答案 2 :(得分:0)

无法添加评论,但我已经看到了包含换行符的字符串。

cell.textLabel.text = [[myString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@" "];

如果myString有换行符,应修复问题。