uitableviewcell中的文字不一致?

时间:2013-01-31 12:52:15

标签: iphone objective-c uitableview

以下是我UItableView的样子:

enter image description here

最后一个单元格可乐未与其他单元格正确对齐。我该如何解决这个问题?

这是我的所有代码:

#import "SearchViewController.h"

@interface SearchViewController ()
{
    NSMutableArray *filteredStrings;
    NSMutableArray *arrayDataFromServer;
    NSMutableArray *totalStrings;
    BOOL isFiltered;
}

@end

@implementation SearchViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.SearchBar.delegate = self;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;


    NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/Search.php?choice=name"];
    NSArray *arrayImagesNames = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];


    arrayDataFromServer = [[NSMutableArray alloc]init];


    NSEnumerator *enumForNames = [arrayImagesNames objectEnumerator];

    id objName;

    while ( objName = [enumForNames nextObject]) {
       [arrayDataFromServer addObject:[NSDictionary dictionaryWithObjectsAndKeys:objName, @"name", nil]];
    }

}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/Search.php?choice=name"];
    NSArray *arrayImagesNames = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];
    NSLog(@"%@", arrayImagesNames);

    if (searchText.length == 0)
    {
        isFiltered = NO;
    }
    else
    {
        isFiltered = YES;

        filteredStrings = [[NSMutableArray alloc]init];

        for (NSString *str in arrayImagesNames)
        {
            NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if (stringRange.location != NSNotFound)
            {
                [filteredStrings addObject:str];
            }
        }
    }
    [self.tableView reloadData];

}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self.SearchBar resignFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (isFiltered)
    {
        return [filteredStrings count];
    }
    return [arrayDataFromServer count];
}

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    if (!isFiltered)
    {

        NSString *nn = [[arrayDataFromServer objectAtIndex:indexPath.row] objectForKey:@"name"];
        cell.textLabel.text = nn;

    }
    else
    {
        cell.textLabel.text = [filteredStrings objectAtIndex:indexPath.row];
    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;
}

我使用SearchBar搜索单元格。从服务器调用单元中的所有信息。我试图使用普通数组..@"1", @"2", @"3"...,nil];并正确对齐文本。

非常感谢任何帮助!提前谢谢!

我使用NSLog显示数组:

enter image description here

2 个答案:

答案 0 :(得分:1)

在设置文本时,请在cellForRowAtIndexPath中尝试以下操作。

cell.textLabel.text = [someText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

最诚挚的问候。

答案 1 :(得分:0)

你可以试试这个,

cell.textAlignment = UITextAlignmentLeft;