0

这是我的UItableView样子:

在此处输入图像描述

最后一个单元格可乐与其他单元格没有正确对齐。我怎样才能解决这个问题?

这是我的所有代码:

#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 来显示数组:

在此处输入图像描述

4

2 回答 2

1

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

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

此致。

于 2013-01-31T13:02:32.337 回答
0

你可以试试这个

cell.textAlignment = UITextAlignmentLeft;
于 2013-01-31T13:16:17.360 回答