Here is how my UItableView
looks like:
The last cell, Coke is not aligned properly as the other cells. How can I fix this?
Here is all my code:
#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;
}
I used the SearchBar to search the cells. All the information in the cells are called from the server. I have tried to used the normal array ..@"1", @"2", @"3"...,nil];
and the text aligned properly.
Any help would be very much appreciated! Thanks in advance!
I used NSLog to display the array: