NSPredicate issue with NSObjects IOS - ios

I have an NSObject:
#interface ContactAlphaB : NSObject
+ (ContactAlphaB *)contactWithFirstName:(NSString *)firstName lastName:(NSString *)lastName username:(NSString *)username
linkAvatar:(NSString *)linkAvatar registerType:(NSString *)registerType
email:(NSString *)email mobile:(NSString *)mobile;
#end
I had save all informations in this object.
#property (nonatomic) NSString *firstName;
#property (nonatomic) NSString *lastName;
#property (nonatomic) NSString *username;
#property (nonatomic) NSString *linkAvatar;
#property (nonatomic) NSString *registerType;
#property (nonatomic) NSString *stremail;
#property (nonatomic) NSString *strmobile;
And I had show in tableView
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.sectionIndexTitles;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.sectionIndexTitles count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *sectionIndexTitle = self.sectionIndexTitles[section];
return [self.alphabetizedDictionary[sectionIndexTitle] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CellListsFriend *cell = [tableView dequeueReusableCellWithIdentifier:#"cellListsFriend" forIndexPath:indexPath];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
ContactAlphaB *contact = [self objectAtIndexPath:indexPath];
cell.lblNameFriend.text = contact.fullName;
// Rounded Rect for cell image
cell.imgAvatarFriends.layer.borderWidth=2;
cell.imgAvatarFriends.layer.borderColor=[[UIColor whiteColor]CGColor];
[cell.imgAvatarFriends.layer setCornerRadius:cell.imgAvatarFriends.frame.size.width/2];
[cell.imgAvatarFriends.layer setMasksToBounds:YES];
return cell;
}
- (void)setContacts:(NSArray *)contacts {
self.alphabetizedDictionary = [CGLAlphabetizer alphabetizedDictionaryFromObjects:contacts usingKeyPath:#"lastName"];
self.sectionIndexTitles = [CGLAlphabetizer indexTitlesFromAlphabetizedDictionary:self.alphabetizedDictionary];
[self.tableView reloadData];
}
- (ContactAlphaB *)objectAtIndexPath:(NSIndexPath *)indexPath {
NSString *sectionIndexTitle = self.sectionIndexTitles[indexPath.section];
return self.alphabetizedDictionary[sectionIndexTitle][indexPath.row];
}
With this code, I can show all myfriend lists in tableView.
And now, I want to do search function (search my friend).
So, I'm using NSPredicate *filter = [NSPredicate predicateWithFormat:#"SELF == %#", searchString]; to work this function.
But, I cannot use filteredArrayUsingPredicate in NSObject to search.
How to I can do search function.
Please help me!
*****************Edited:
NSArray *arr = [FriendList MR_findAll];
if (arr.count > 0) {
for (int i = 0; i < arr.count; i++) {
FriendList *friendList = [arr objectAtIndex:i];
[delegate.arrFriendListsAdded addObject:friendList.username];
ContactAlphaB *contact = [ContactAlphaB contactWithFirstName:friendList.firstName
lastName:friendList.lastName
username:friendList.username
linkAvatar:friendList.avatar
registerType:[NSString stringWithFormat:#"%#", friendList.registerType]
email:friendList.email
mobile:friendList.phoneNumber
];
[_mucontacts addObject:contact];
}
}
_mucontacts is NSMutableArray.
When I log this array:
<__NSArrayM 0x4e48050>(
<ContactAlphaB: 0x4f55b90>,
<ContactAlphaB: 0x13e8dc0>,
<ContactAlphaB: 0x4f2bd30>,
<ContactAlphaB: 0x4e6a490>
)
It is save object. When Search, I want to search lastname.
So, When I search lastname it is get empty.

try with this
NSString *searchString = #"your last name here";
NSPredicate *lastname = [NSPredicate predicateWithFormat:#"self.lastname contains[cd] %#", searchString];
NSArray *filteredArray = [mucontacts filteredArrayUsingPredicate:lastname];

Related

Update UI table View from a Static method

I am developing a chat application. Some of my classes are singleton therefore I have use lot of static methods.
When ever a new message is received in app delegate. It should send it to my incomingChat viewController.
I am able to get the new message to static method in viewcontroller. But I cant reload the table from static method.
InCommingVC.h
#import <UIKit/UIKit.h>
#interface InCommingVC : UIViewController
#property (weak, nonatomic) IBOutlet UINavigationBar *navigationBarTitle;
#property (weak, nonatomic) IBOutlet UITableView *incommingTable;
+ (void) sendIncommingChats:(NSDictionary *) chatDetails;
+ (void) recieveIncomingChat:(NSDictionary *) chatDetails;
#end
InCommingVC.m
#import "InCommingVC.h"
#import "AppDelegate.h"
#import "IncommingItemObject.h"
static NSMutableArray *incomminglist;
#interface InCommingVC (){
AppDelegate *delegate;
}
#end
#implementation InCommingVC
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationBarTitle.topItem.title = #"Incomming Chats";
}
+ (void) recieveIncomingChat:(NSDictionary *) chatDetails{
NSLog(#"GOT A NEW recieveIncomingChat");
NSString *CompanyId = [chatDetails objectForKey:#"CompanyId"];
NSString *ConnectionId = [chatDetails objectForKey:#"ConnectionId"];
NSString *CountryCode = [chatDetails objectForKey:#"CountryCode"];
NSString *Department = [chatDetails objectForKey:#"Department"];
NSString *Name = [chatDetails objectForKey:#"Name"];
NSString *StartTime = [chatDetails objectForKey:#"StartTime"];
NSString *TimeZone = [chatDetails objectForKey:#"TimeZone"];
NSString *VisitorID = [chatDetails objectForKey:#"VisitorID"];
NSString *WidgetId = [chatDetails objectForKey:#"WidgetId"];
NSLog(#"------------------------------------------------------------------------------");
NSLog(#"CompanyId : %#" , CompanyId);
NSLog(#"ConnectionId : %#" , ConnectionId);
NSLog(#"CountryCode : %#" , CountryCode);
NSLog(#"Department : %#" , Department);
NSLog(#"Name : %#" , Name);
NSLog(#"StartTime : %#" , StartTime);
NSLog(#"TimeZone : %#" , TimeZone);
NSLog(#"VisitorID : %#" , VisitorID);
NSLog(#"WidgetId : %#" , WidgetId);
NSLog(#"------------------------------------------------------------------------------");
IncommingItemObject *item = [[IncommingItemObject alloc] init];
[item setCompanyId:CompanyId];
[item setConnectionId:ConnectionId];
[item setCountryCode:CountryCode];
[item setDepartment:Department];
[item setName:Name];
[item setStartTime:StartTime];
[item setTimeZone:TimeZone];
[item setVisitorID:VisitorID];
[item setWidgetId:WidgetId];
if (incomminglist.count == 0) {
incomminglist = [[NSMutableArray alloc] init];
[incomminglist addObject:item];
[[InCommingVC incommingTable] reloadData];
} else {
[incomminglist addObject:item];
}
NSLog(#"count %i", incomminglist.count);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return incomminglist.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellIdentifier = #"identify_incomming";
UITableViewCell *cell = [self.incommingTable dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
IncommingItemObject *item = [incomminglist objectAtIndex:indexPath.row];
UIImageView *CountryImage = (UIImageView *)[cell viewWithTag:5010];
[CountryImage setImage:[UIImage imageNamed:item.CountryCode]];
UILabel *visitorName = (UILabel *)[cell viewWithTag:5011];
visitorName.text = item.Name;
UILabel *visitStartTime = (UILabel *)[cell viewWithTag:5012];
visitStartTime.text = item.StartTime;
return cell;
}
I want to update my incommingTable from a static method. can some one help me. tnx.
I am having this error
/Users/zupportdesk/Desktop/MyIOSApps/Chat System/Chat
System/InCommingVC.m:96:23: No known class method for selector
'incommingTable'
while doing
[[InCommingVC incommingTable] reloadData];
2 ways :
1 - Make a shared instance. Call :
[[[self class] sharedInstance].tableView reloadData];
2 - Make you class confirm to some notification , that you'll send upon receiving message with payload (chat dictionary). Make sure to deregister the notification when view controller de-allocates
[self.incommingTable reloadData];
Try this please and make sure IBOUTLET is proper connected

Thread 1 : signal SIGABRT

I am very new to Objective.I am trying to make a simple application and after adding more view I get this error "Thread 1 Signal SIGABRT" and the app wont open in iOS Simulator. The error points to this line of code:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
I tried searching before but I didn't understand what the other answers were talking about.
NamesTableViewController.h is :
#import <UIKit/UIKit.h>
#interface NamesTableViewController : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>
#property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
#end
NamesTableViewController.m is:
#import "NamesTableViewController.h"
#interface NamesTableViewController ()
#property (nonatomic, copy) NSDictionary *propertyList;
#property (nonatomic, copy) NSArray *letters;
#property (nonatomic, copy)NSMutableArray *filteredNames;
#property (nonatomic, strong)UISearchController *searchController;
#end
#implementation NamesTableViewController
#synthesize propertyList, letters, filteredNames, searchController;
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tableView = (id)[self.view viewWithTag:1];
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
filteredNames = [[NSMutableArray alloc]init];
searchController = [[UISearchController alloc]init];
self.searchController.searchResultsUpdater = self;
NSString *path = [[NSBundle mainBundle] pathForResource:#"names" ofType:#"plist"];
self.propertyList = [NSDictionary dictionaryWithContentsOfFile:path];
self.letters = [[self.propertyList allKeys] sortedArrayUsingSelector:#selector(compare:)];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView.tag == 1){
return self.letters.count;
}else {
return 1;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView.tag == 1) {
NSString *letter = self.letters[section];
NSArray *keyValues = [self.propertyList[letter] allKeys];
return keyValues.count;
} else {
return [filteredNames count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
if (tableView.tag == 1){
NSString *letter = self.letters[indexPath.section];;
NSArray *keyValues = [[self.propertyList[letter] allKeys] sortedArrayUsingSelector:#selector(compare:)];
cell.textLabel.text = keyValues[indexPath.row];
} else{
cell.textLabel.text = filteredNames[indexPath.row];
}
return cell;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.letters;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (tableView.tag == 1) {
return letters [section];
} else {
return nil;
}
}
#pragma mark Search Display Delegate Methods
-(void)searchDisplayController:(UISearchController *)controller didLoadSearchResultsTableView:(UITableView *)tableView {
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
}
-(BOOL)searchDisplayController:(UISearchController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[filteredNames removeAllObjects];
if (searchString.length > 0) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF contains [search] %#", self.searchBar.text];
for (NSString *letter in letters) {
NSArray *matches = [[self.propertyList[letter] allKeys]filteredArrayUsingPredicate:predicate];
[filteredNames addObjectsFromArray:matches];
}
}
return YES;
}
#end
If you want more information just say it to me by answers and I will edit my question and then you will edit your answer
[keys allKeys] returns an single-level array. Therefore there can be only one section.
Edit:
For better understanding and readability I replaced names with letters and keys with propertyList
• .h
The methods numberOfRowsInSection and cellForRowAtIndexPath retrieve the section letter and then the appropriate keys from the property list.
A dictionary as data source is not the best choice because there are many repetitive tasks to perform for example the sorting. Better create an nested array as data source in viewDidLoad
#interface NamesTableViewController ()
#property (nonatomic, copy) NSDictionary *propertyList;
#property (nonatomic, copy) NSArray *letters;
#end
• .m
#implementation NamesTableViewController
#synthesize propertyList, letters;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"names" ofType:#"plist"];
self.propertyList = [NSDictionary dictionaryWithContentsOfFile:path];
self.letters = [[self.propertyList allKeys] sortedArrayUsingSelector:#selector(compare:)];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.letters.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSString *letter = self.letters[section];
NSArray *keyValues = [self.propertyList[letter] allKeys];
return keyValues.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
NSString *letter = self.letters[indexPath.section];
NSArray *keyValues = [[self.propertyList[letter] allKeys] sortedArrayUsingSelector:#selector(compare:)];
cell.textLabel.text = keyValues[indexPath.row];
return cell;
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return self.letters;
}
#end

Exception while typing into search bar on iOS

I'm taking over some code since another dev has moved on and his search bar has an error. I've searched the other threads for help on this, but none of them seem to be my error. Here is a screen shot of mine and the code.
http://imgur.com/gO6wuPb
2014-09-17 11:25:34.665 Moca[11388:60b] * Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key itemName.'
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];
} else {
return [[[BNRItemStore sharedStore] allItems] count];
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 71;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"UITableViewCell";
UITableViewCell *cell = (UITableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//there are two tableviews involved here - one is the main one, and one is associated with searchDisplayController
//self.tableView is set automatically between the main one and the search one.
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSArray *items = [[BNRItemStore sharedStore] allItems];
BNRItem *item = nil;
if (tableView == self.searchDisplayController.searchResultsTableView) {
item = [searchResults objectAtIndex:indexPath.row];
} else {
item = items[indexPath.row];
}
if ([item.subcategory isEqual:#"Urgent"]) {
cell.imageView.image=[UIImage imageNamed:#"Urgent.png"];
}
else if ([item.subcategory isEqual:#"Emergency"]) {
cell.imageView.image=[UIImage imageNamed:#"Emergency.png"];
}
else if ([item.subcategory isEqual:#"Standard"]) {
cell.imageView.image=[UIImage imageNamed:#"Standard.png"];
}
else if ([item.subcategory isEqual:#"Normal"]) {
cell.imageView.image=[UIImage imageNamed:#"Normal.png"];
}
else {
cell.imageView.image=[UIImage imageNamed:#"Informational.png"];
}
cell.textLabel.text = item.changeOrder;
cell.detailTextLabel.text =item.title;
return cell;
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSArray *items = [[BNRItemStore sharedStore] allItems];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:#"itemName contains[c] %#", searchText];
searchResults = [items filteredArrayUsingPredicate:resultPredicate];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];
return YES;
}
EDIT add item details
#import <Foundation/Foundation.h>
#interface BDDetailedChange : NSObject
+ (instancetype)randomItem;
- (instancetype)initWithItemName:(NSString *)name
valueInDollars:(NSString *)value
serialNumber:(NSString *)sNumber;
#property (nonatomic, copy) NSString *itemName;
#property (nonatomic, copy) NSString *serialNumber;
#property (nonatomic, copy) NSString *valueInDollars;
#property (nonatomic, readonly, strong) NSDate *dateCreated;
#property (nonatomic, copy) NSString *subcategory; //Emergency
#property (nonatomic, copy) NSString *service;
#property (nonatomic, copy) NSString *associatedCIs;
#property (nonatomic, copy) NSString *environment;// Production
#property (nonatomic, copy) NSString *approvalGroup;
#property (nonatomic, copy) NSString *initiator;
#property (nonatomic, copy) NSString *coordinator;
#property (nonatomic, copy) NSString *riskLevel;// Low
#property (nonatomic, copy) NSString *performingGroup;
#property (nonatomic, copy) NSString *implementationPlan;
#property (nonatomic, copy) NSString *validationPlan;
#property (nonatomic, copy) NSString *recoveryScope;
#end
BNR item .h
#interface BNRItem : NSObject
{
NSString *_approverEid;
NSString *_assignmentGroup;
NSString *_changeOrder;
NSString *_subcategory; //Emergency
NSString *_title; //title
}
- (void)setApproverEid: ( NSString *)aEid;
- (NSString*)approverEid;
- (void)setAssignmentGroup: ( NSString *)aGrp;
- (NSString*)assignmentGroup;
- (void)setChangeOrder: ( NSString *)co;
- (NSString*)changeOrder;
- (void)setSubcategory: ( NSString *)sub;
- (NSString*)subcategory;
- (void)setTitle: ( NSString *)title;
- (NSString*)title;
#end
BNRItem.m
#import "BNRItem.h"
#implementation BNRItem
- (void)setApproverEid:(NSString *)aEid
{
_approverEid = aEid;
}
- (void)setAssignmentGroup: ( NSString *)aGrp
{
_assignmentGroup = aGrp;
}
- (void)setChangeOrder: ( NSString *)co
{
_changeOrder=co;
}
- (void)setSubcategory: ( NSString *)sub
{
_subcategory= sub;
}
- (void)setTitle: ( NSString *)title
{
_title = title;
}
- (NSString*)approverEid
{
return _approverEid;
}
- (NSString*)assignmentGroup
{
return _assignmentGroup;
}
- (NSString*)changeOrder
{
return _changeOrder;
}
- (NSString*)subcategory
{
return _subcategory;
}
- (NSString*)title
{
return _title;
}
#end

Indexed Table View for Facebook Friends

I am trying to sort my Facebook friends list in order to implement an indexed table view, exactly like the one found in Facebook Messenger.
I originally tried to use the UILocalizedIndexedCollation but I am stuck on the "collationStringSelector" because my dataSource array are id<"FBGraphUser"> objects and therefore have no properties I can use for the selector. Any ideas of how to implement this (it does not have to be using this method, I am open to anything!)?
-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector {
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionCount = [[collation sectionTitles] count]; // section count is take from sectionTitles and not sectionIndexTitles
NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
// create an array to hold the data for each section
for (int i = 0; i < sectionCount; i++) {
[unsortedSections addObject:[NSMutableArray array]];
}
// put each object into a section
for (id object in array) {
NSInteger index = [collation sectionForObject:object collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}
NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
// sort each section
for (NSMutableArray *section in unsortedSections) {
[sections addObject:[collation sortedArrayFromArray:section collationStringSelector:selector]];
}
return sections;
}
Here is a subclass of UITableViewController that I created some time ago to make an indexed table view. I use this as a base class for the controller whose table view appears on screen, so it's intended to be a reusable class that does the hard work of creating the table. You might be able to use it as is, but I didn't try to make it too universal. I pass in a simple array of custom objects (my objects just had first and last names), and this class creates the sections and the index. So here's the code,
the .h
#interface RDIndexedTableController : UITableViewController
#property (strong,nonatomic) NSArray *inputArray;
#property (strong,nonatomic) NSString *sectionKey;
#property (strong,nonatomic) NSString *secondarySortKey;
#property (nonatomic, retain) NSMutableArray *sectionsArray;
#property (nonatomic, retain) UILocalizedIndexedCollation *collation;
-(void)convertArray:(NSArray *)input usingSectionKey:(NSString *)key secondarySortKey:(NSString *)sorter;
#end
The .m
#interface RDIndexedTableController ()
#property (strong,nonatomic) NSMutableArray *firstLetterArray;
#end
#implementation RDIndexedTableController
-(void)convertArray:(NSArray *)input usingSectionKey:(NSString *)key secondarySortKey:(NSString *)sorter {
self.inputArray = input;
self.sectionKey = key;
self.secondarySortKey = sorter;
[self configureSections];
}
- (void)configureSections {
self.collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionTitlesCount = [[self.collation sectionTitles] count];
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
for (int index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[newSectionsArray addObject:array];
}
for (id obj in self.inputArray) {
NSInteger sectionNumber = [self.collation.sectionTitles indexOfObject:[[obj valueForKey:self.sectionKey] substringToIndex:1]];
NSMutableArray *sectionForObjects = [newSectionsArray objectAtIndex:sectionNumber];
[sectionForObjects addObject:obj];
}
for (int index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *objectArrayForSection = [newSectionsArray objectAtIndex:index];
NSArray *firstSortedObjectArrayForSection = [self.collation sortedArrayFromArray:objectArrayForSection collationStringSelector:NSSelectorFromString(self.secondarySortKey)];
NSArray *sortedObjectArrayForSection = [self.collation sortedArrayFromArray:firstSortedObjectArrayForSection collationStringSelector:NSSelectorFromString(self.sectionKey)];
[newSectionsArray replaceObjectAtIndex:index withObject:sortedObjectArrayForSection];
}
self.sectionsArray = newSectionsArray;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.collation.sectionTitles.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *objectsInSection = [self.sectionsArray objectAtIndex:section];
return objectsInSection.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return self.collation.sectionTitles[section];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if ([[self.sectionsArray objectAtIndex:section] count] == 0) {
return 0;
}else{
return 30;
}
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return #[#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z"];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [self.collation sectionForSectionIndexTitleAtIndex:index];
}
In the table view controller that appears in the app, I only need this small amount of code to populate the table,
-(void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
NSMutableArray *mut = [NSMutableArray new];
// create the Person objects and add them to the array here
[self convertArray:mut usingSectionKey:#"lastName" secondarySortKey:#"firstName"];
[self.tableView reloadData];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *first = [self.sectionsArray[indexPath.section][indexPath.row] valueForKey:#"firstName"];
NSString *last = [self.sectionsArray[indexPath.section][indexPath.row] valueForKey:#"lastName"];
cell.textLabel.text = [NSString stringWithFormat:#"%# %#",first,last];
return cell;
}

Memory keep increasing when the UITableView scrolling

there is a strange problem I have not met ever
there is an array() including some custom object named MyClass parsed by JSONKit;
when I keep scrolling the tableview the memory will keeping increasing too.
but when replace
cell.textLabel.text = myclass.name;
with
cell.textLabel.text = #"cool";
or
cell.textLabel.text = [NSString stringWithFormate:#"a-%d", indexPath.row];
it's ok the memory with keep stable
but if I use
cell.textLabel.text = [NSString stringWithFormate:#"a-%#-i",myclass.name, indexPath.row];
it also keep increasing;
It will drive my crazy!!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Singers";
OMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
MyClass *myclass = [self.data objectAtIndex:indexPath.row];
if (cell == nil){
cell = [[[OMTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
cell.textLabel.text = myclass.name;
return cell;
}
MyClass
there is two class one Base another inherit
Base:
#interface OMBase : NSObject {
NSMutableDictionary *data;
NSString *name;
NSArray *keys;
}
#property (nonatomic, retain) NSString *name;
#property (nonatomic, copy) NSMutableDictionary *data;
#implementation OMBase
#synthesize data, name;
- (void)setData:(NSMutableDictionary *)adata{
if (data){
[data release];
data = nil;
}
data = [adata mutableCopy];
}
- (void)dealloc{
if (keys){
[keys release];
}
[data release];
[super dealloc];
}
- (id)init{
if (self = [super init]){
self.data = [[[NSMutableDictionary alloc] initWithCapacity:20] autorelease];
}
return self;
}
inherit:
#import "OMBase.h"
#interface OMLyric : OMBase
- (NSString *)songid;
- (NSString *)content;
#import "OMLyric.h"
#implementation OMLyric
- (NSString *)songid{
return [data objectForKey:#"songid"];
}
- (NSString *)content{
return [data objectForKey:#"content"];
}
Seems like your myclass.name getter returns a new allocated object. We can't say more without seeing myclass.

Resources