UITableViewController add item via method - ios

I am using a UISplitviewController and I am trying to add items to the table view.
right now I have two ways
Create a button and add it:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
and this code runs when the button is clicked:
- (void)insertNewObject:(id)sender {
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
and adds an item to the table view:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
this way works.
This is what I am trying to do:
- (void)GetRequest
{
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
but this does not update the table view. I added a breakpoint and when I use the button I added, it goes into the table view, with the method it does not and I am calling the method via [MasterController GetRequest];
What am I doing wrong ?
I am calling GetRequest from another controller.
This is how MasterController is getting defined:
#interface DetailController ()
{
MasterController *MasterController;
}
DetailController.m:
#import "MasterController.h"
#interface DetailController ()
{
MasterController *MasterController;
}
#end
#implementation DetailController
-(void)viewDidLoad {
MasterController = [[MasterController alloc]init];
}
[MasterController GetRequest];
MasterController.m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (void)GetRequest
{
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
[self.tableView reloadData];
}

As I suspected, with this line, MasterController = [[MasterController alloc]init], you're creating a new instance that has nothing to do with the one you see on screen. You need to get a reference to the master controller that you already have in the split view controller. From the detail view controller, you can get that like so,
MasterController = self.splitViewController.viewControllers.firstObject;
The split view controller has a viewControllers property, and the one at index 0 is the master, and the one at index 1 is the detail. BTW, you should start your ivars and method names with a lower class letter.

UITableViewController doesn't really work that way.
If you subclass it you need to implement several methods that tells the table what data it has -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
To add new rows you just need to add objects to self.objects and call [self.tableView reloadData];
It'll basically be -
- (void)GetRequest
{
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
[self.tableView reloadData];
}

Related

showing NSMutabelArray in tableViewController in objective c

I am passing NSMutableArray to another a tableview and I want to show it in the table view. My NSMutableArray is as follows
2017-02-07 18:32:24.086 krib[13753:2978659] (
"Balcony.png",
"Utilities Included.png",
"Air-conditioning.png",
"Stove.png",
"WiFi Included.png",
"Queen Bed.png",
"Dining Table.png",
"Washing Machine.png",
"Dryer.png",
"Sofa.png",
"TV.png",
"Curtains.png",
"Refrigerator.png",
"Water Heater.png",
"Microwave Oven.png"
)
I send data as,
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"segue"]) {
MZFormSheetPresentationViewControllerSegue *presentationSegue = (id)segue;
presentationSegue.formSheetPresentationController.presentationController.shouldApplyBackgroundBlurEffect = YES;
UINavigationController *navigationController = (id)presentationSegue.formSheetPresentationController.contentViewController;
AmennitiesTableTableViewController *presentedViewController = [navigationController.viewControllers firstObject];
// presentedViewController.textFieldBecomeFirstResponder = YES;
presentedViewController.passingString = facilities;
}
and receive data in table view controller,
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStylePlain target:self action:#selector(close)];
NSLog(#"%#",self.passingString);
[self.tableView reloadData];
}
I tried showing the mutable array as follows,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"facilitiesCell";
AmenitiesTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.facilitylbl.text = [self.passingString objectAtIndex:indexPath.row];
// Configure the cell...
return cell;
}
But I am not understanding what exactly i am missing to Populate data on the tableview.
I think you are missing to set tableview delegate & datasource.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStylePlain target:self action:#selector(close)];
[self.myTable setDelegate:self];
[self.myTable setDataSource:self];
NSLog(#"%#",self.passingString);
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return self.passingString.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"facilitiesCell";
AmenitiesTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(self.passingString.count > 0){
cell.facilitylbl.text = [self.passingString objectAtIndex:indexPath.row];
}
// Configure the cell...
return cell;
}

Tapping the cell selections does not change the cell accessory to a checkmark

Can someone help me? I've been following along with Apple's official Objective C tutorial (the one that has us create a To Do List).
I'm stuck at the part where tapping the cell item displays/removes a checkmark. It's not happening.
Here's my implementation file if someone could take a look?
I've really tried my best to solve where I went wrong. I swear I tried my best to figure out everything on my own before posting here. Please, any help would be greatly appreciated!!
Thank you!
EDIT: So i put a breakpoint in my didSelectRowAtIndexPath method. It was not triggered when I tapped a table item, so it's not getting called when I tap items, right? Where do I look next?
//
// ToDoListTableViewController.m
// ToDoList
//
// Created by Kevin Zagala on 9/1/14.
// Copyright (c) 2014 Kevin Zagala. All rights reserved.
//
#import "ToDoListTableViewController.h"
#import "ToDoItem.h"
#interface ToDoListTableViewController ()
#property NSMutableArray *toDoItems;
#end
#implementation ToDoListTableViewController
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{
}
- (void)loadInitialData {
ToDoItem *item1 = [[ToDoItem alloc] init];
item1.itemName = #"Buy milk";
[self.toDoItems addObject:item1];
ToDoItem *item2 = [[ToDoItem alloc] init];
item2.itemName = #"Buy eggs";
[self.toDoItems addObject:item2];
ToDoItem *item3 = [[ToDoItem alloc] init];
item3.itemName = #"Read a book";
[self.toDoItems addObject:item3];
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
// 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)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.toDoItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
ToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
if (toDoItem.completed) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
#pragma mark - Table view delegate
- (void)tableview:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
ToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
tappedItem.completed = !tappedItem.completed;
[tableView reloadRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
#end
try this ..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ListPrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
ToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
try reloading whole tableview,
what u are doing should work, i tested your code by putting a dummy object with boolean value, it worked for me,
Note Both reloading the whole tableview and reloading the selected cell will work, only u are setting the correct boolean value
- (void)tableview:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
ToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
tappedItem.completed = !tappedItem.completed;
// [tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone]; //comment this
[tableView reloadData]; //add this
}

custom cell not being displayed

So i created a custom cell (using the .xib file) and linked it using a custom controller class and I also didn't forget to write in the cell identifier. I also gave the same cell identifier in my table view controller prototype cell. In the custom cell controller class I just have an outlet to a text label in the .h file. Below is the code for my table view controller. When I run the app, the custom cells are not displayed but there are cells there because i can click on them (but they are just white). What am I doing wrong, why aren't the custom cells displaying?
If I use the default cells (not custom), then everything works fine. So the problem is that I'm not using my custom cells correctly somewhere.
#import "ListmaniaTableViewController.h"
#import "ListmaniaTableViewCell.h"
#interface ListmaniaTableViewController ()
#property (strong, nonatomic) NSMutableArray *tasks; //of task object
#end
#implementation ListmaniaTableViewController
- (void)viewDidLoad{
[super viewDidLoad];
task *task1 = [[task alloc] init];
task1.taskDescription = #"TASK 1";
[self.tasks addObject:task1];
task *task2 = [[task alloc] init];
task2.taskDescription = #"TASK 2";
[self.tasks addObject:task2];
task *task3 = [[task alloc] init];
task3.taskDescription = #"TASK 3";
[self.tasks addObject:task3];
}
- (NSMutableArray *)tasks{
if(!_tasks){
_tasks = [[NSMutableArray alloc] init];
}
return _tasks;
}
/*- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"Add New Item"]) {
}
}
- (void)addNewTask:(task *)newTask{
[self.tasks addObject:newTask];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.tasks count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ListmaniaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ListmaniaCell" forIndexPath:indexPath];
if(!cell){
[tableView registerNib:[UINib nibWithNibName:#"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:#"ListmaniaCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"ListmaniaCell" forIndexPath:indexPath];
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(ListmaniaTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
task *task = [self.tasks objectAtIndex:indexPath.row];
NSString *taskLabel = task.taskDescription;
cell.taskLabel.text = taskLabel;
}
#end
But the
[self.tableView registerNib:[UINib nibWithNibName:#"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:#"ListmaniaCell"];
in viewDidLoad:.
The code you have in tableView:willDisplayCell:forRowAtIndexPath: method should be in tableView:cellForRowAtIndexPath: method. Like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
ListmaniaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ListmaniaCell" forIndexPath:indexPath];
if(!cell){
[tableView registerNib:[UINib nibWithNibName:#"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:#"ListmaniaCell"];
cell = [tableView dequeueReusableCellWithIdentifier:#"ListmaniaCell" forIndexPath:indexPath];
}
task *task = [self.tasks objectAtIndex:indexPath.row];
NSString *taskLabel = task.taskDescription;
cell.taskLabel.text = taskLabel;
return cell;
}
I figured out the problem. I had an outlet in the custom cell that was doubly linked. I also moved the line below into viewdidload as suggested by #dasdom
[self.tableView registerNib:[UINib nibWithNibName:#"ListmaniaTableViewCell" bundle:nil] forCellReuseIdentifier:#"ListmaniaCell"];

Inserting items in UITableView crashes application

I'm making an application with a tableview list of contacts that can be reached via a tab-controller at the bottom.
I copied (literally copy/pasted) from the example Master Detail Application and tried to make sure all storyboard references lined up.
#import "ContactsTableViewController.h"
#import "ContactViewController.h"
#interface ContactsTableViewController () {
NSMutableArray *_objects;
}
#end
#implementation ContactsTableViewController
- (void)awakeFromNib
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320.0, 600.0);
}
[super awakeFromNib];
// [self.tableView setDelegate:self]; // From (unsuccesfully) trying https://stackoverflow.com/questions/16311393/how-to-insert-items-to-a-uitableview-when-a-uibutton-is-clicked-in-ios
// [self.tableView setDataSource:self];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.contactViewController = (ContactViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; // Crashes here
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath]; // Crashes here
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[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;
}
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
NSDate *object = _objects[indexPath.row];
self.contactViewController.detailItem = object;
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
#end
It crashes on the first line in CellRowAtIndexPath. Since I was having trouble I also took the advice in How to insert items to a UITableView when a UIButton is clicked in iOS but it didn't solve my problem.
This is just incredibly frustrating, because as far as I can tell my code is (except for names) exactly the same as the example application.
edit: Exception message is
'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
Alex - When using storyboards and the new prototype cell feature in xCode, you have to set an identifier value in Interface Builder whose value should match what is in your code.
So notice you have this line:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
In this case, your cell identifier is "Cell"
Can you confirm that in Interface Builder/Storyboard, your table view cell's identifier is set to the same?
As an example, here's a screenshot from a sample app I was building (Notice my Indentifier field on the right):
I would recommend this
-(void)ViewDidLoad{
[tableView registerClass:[MyCell class] forCellReuseIdentifier:#"Cell"];
}
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
[tableView reloadData]; //this will trigger cellForRowAtIndexPath again with the updated array
}
and would you please post your error.
You should not use [self.tableView insertRowsAtIndexPaths: withRowAnimation:];. Instead of this you should call [talbeView reloadData] method.

trying to get a 'Search Bar and Display Controller' to repopulate a table view

I am trying to get the 'Search Bar and Display Controller' functionality to work in an iOS app. I am able to NSLog in repsonse to a search query and hardcode in a new array but am unable to get the table view to repopulate. I have the following for in response to a user submit on the search button:
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
NSLog(#"You clicked the search bar");
NSMutableArray *filteredResults=[[NSMutableArray alloc] init];
Person *filteredPerson=[[Person alloc] init];
filteredPerson.firstName=#"Josie - here i am";
[filteredResults addObject:filteredPerson];
_objects=filteredResults;
self.tableView.dataSource = _objects;
[self.tableView reloadData];
}
Any ideas on making this repopulate would be appreciated.
thx
edit #1
It looks like this is populating the _objects NSMutableArray:
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
Should I just create a new _objects and use insertNewObject rather than the addObject code I have above? Would this bypass the need to deal with the dataSource property of the table view?
edit 2
per #ian
- (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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
/*
NSDate *object = [_objects objectAtIndex:indexPath.row];
cell.textLabel.text = [object description];
*/
Person *rowPerson = [_objects objectAtIndex:indexPath.row];
cell.textLabel.text = [rowPerson firstName];
return cell;
}
thx
I have a UITableView that uses an NSMutableArray to hold the data. Here's how it works: set the UISearchDisplayController's delegate to your TableView controller, and when the UITableViewDelegate methods are called (numberOfRows, numberOfSections, cellForRowAtIndexPath, etc) you can do the following to serve up the search data when appropriate:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger numberOfRows = 0;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
//This is your search table --- use the filteredListContent (this is the NSMutableArray)
numberOfRows = [filteredListContent count];
}
else
{
//Serve up data for your regular UITableView here.
}
return numberOfRows;
}
You should take a look at the UISearchDisplayDelegate documentation. You can use these methods to update your filteredListContent array, as follows:
#pragma mark -
#pragma mark Content Filtering
- (void)filterContentForSearchText:(NSString*)searchText
{
//In this method, you'll want to update your filteredListContent array using the string of text that the user has typed in. For example, you could do something like this (it all depends on how you're storing and retrieving the data):
NSPredicate *notePredicate = [NSPredicate predicateWithFormat:#"text contains[cd] %#", searchText];
self.filteredListContent = [[mainDataArray filteredArrayUsingPredicate:notePredicate] mutableCopy];
}
#pragma mark UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
The line self.tableView.dataSource = _objects; is setting your array as the datasource for the UITableView. I assume that you don't have an NSArray subclass that implements the UITableViewDataSource protocol?
Try removing that line, and letting your existing datasource handler deal with the change in data.

Resources