Sort UITableVIew after new string added - ios

I need sort my UITableView when newly generated random strings are added.
I have this code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addRandomString)];
self.navigationItem.rightBarButtonItem = addButton;
UIBarButtonItem *sortButton = [[UIBarButtonItem alloc] initWithTitle:#"Sort" style:UIBarButtonItemStyleBordered target:self action:#selector(sortStrings)];
self.navigationItem.leftBarButtonItem = sortButton;
stringsArray = [[NSMutableArray alloc]init];
int string_lenght = 10;
NSString *symbols = #"ABCDEFGHIJKLMNOPQRSTUWXYZabcdefghijklmnopqrstuvwxyz";
for (int y = 0; y<8; y++)
{
NSMutableString *randomString = [NSMutableString stringWithCapacity:string_lenght];
for (int i = 0; i<string_lenght; i++)
{
[randomString appendFormat:#"%C", [symbols characterAtIndex:random()%[symbols length]]];
}
[stringsArray addObject:randomString];
}
stringsForTableView = [[NSMutableArray alloc]initWithArray:stringsArray];
}
- (void) addRandomString {
int string_lenght = 10;
NSString *symbols = #"ABCDEFGHIJKLMNOPQRSTUWXYZabcdefghijklmnopqrstuvwxyz";
NSMutableString *randomString = [NSMutableString stringWithCapacity:string_lenght];
for (int i = 0; i<string_lenght; i++)
{
[randomString appendFormat:#"%C", [symbols characterAtIndex:random()%[symbols length]]];
}
[stringsForTableView addObject:randomString];
[self.tableView reloadData];
}
- (void) sortStrings{
??????????????????????
}
Thanks for your help!

Use the standard sort functions for mutable arrays:
[stringsForTableView sortUsingDescriptors:
#[[NSSortDescriptor sortDescriptorWithKey:#"self" ascending:YES]]];
[self.tableView reloadData];

Related

UIPopoverController is not so with a color

Does anyone know why is appearing white part? My View is already gray, but gets two white pieces
whites: Arrow and final Popover!
[UPDATE]
this is the code that calls the popover and makes the arrow points to the button that was clicked!
- (void) buttonFilter {
if (viewFilter == #"Artistas") {
content = [self.storyboard instantiateViewControllerWithIdentifier:#"TipoArtistaViewController"]; // MUDAR PARA O NOVO FILTRO DE ARTISTAS
} else if (viewFilter == #"Músicas") {
content = [self.storyboard instantiateViewControllerWithIdentifier:#"CategoriaViewController"];
}
[self callFilter:btnFilter Filter:content];
}
- (void)callFilter:(id)sender Filter:(UIViewController *) content{
self.currentPop = popoverController;
popoverController = [[WYPopoverController alloc] initWithContentViewController:content];
UIButton * bt = (UIButton * )sender;
UIView *view = [bt valueForKey:#"view"];
popoverController.popoverContentSize = CGSizeMake(320, 180);
popoverController.delegate = self;
[popoverController presentPopoverFromRect:view.bounds inView:view permittedArrowDirections:WYPopoverArrowDirectionAny animated:YES];
}
the next is where to mount the session:
//extend and collpase
- (void)setupViewController {
categoriaBD = [categoriaDAO selectCategoria];
self.data = [[NSMutableArray alloc] init];
for (int i = 0; i < [categoriaBD count]; i++)
{
NSMutableDictionary * teste = [categoriaBD objectForKey:[NSString stringWithFormat:#"%i", i]];
ID = [[teste objectForKey:#"1"] integerValue];
subcategoriaBD = [categoriaDAO selectSubCategoriaByCategoriaID:ID];
NSMutableArray* section = [[NSMutableArray alloc] init];
for (int j = 0; j < [subcategoriaBD count]; j++)
{
NSMutableDictionary * subCat = [subcategoriaBD objectForKey:[NSString stringWithFormat:#"%i", j]];
[section addObject:[NSString stringWithFormat:[subCat objectForKey:#"1"]]];
}
[self.data addObject:section];
}
self.headers = [[NSMutableArray alloc] init];
for (int i = 0; i < [categoriaBD count]; i++)
{
NSString *inStr = [NSString stringWithFormat: #"%i", (int)i];
nomeCategoria = [categoriaBD objectForKey:inStr];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 310, 40)];
[label setText:[nomeCategoria objectForKey:#"2"]];
UIView* header = [[UIView alloc] init];
[header setBackgroundColor:[UIColor colorWithRed:(226/255.0) green:(226/255.0) blue:(226/255.0) alpha:1]];
[header addSubview:label];
[self.headers addObject:header];
}
}
You can to create a custom UIPopoverBackgroundView subclass that sets the properties of the arrow you want.
popoverController.popoverBackgroundViewClass = [MyPopoverBackgroundView class];

Passing data to BTsidemenu

I have developed an application in which I am using this: BTSimpleSideMenu
In my app I am passing cell label's through this:
-(void)show{
sideMenu.delegate = self;
int count;
count = [rssOutputData count];
for (int i = 0; i < count; i++){
NSString *items = [[rssOutputData objectAtIndex:i]xmltitle];
BTSimpleMenuItem *item = [[BTSimpleMenuItem alloc]initWithTitle:[[rssOutputData objectAtIndex:i]xmltitle] image:[UIImage imageNamed:#"arrow.png"]
onCompletion:^(BOOL success, BTSimpleMenuItem *item) {
catLbl.text = [[rssOutputData objectAtIndex:i]xmltitle];
}];
sideMenu = [[BTSimpleSideMenu alloc]initWithItem:#[item] addToViewController:self];
}
[sideMenu toggleMenu];
}
In this code [[rssOutputData objectAtIndex:i]xmltitle] is actually the data which I am parsing from an XML file. I have a total of 5 entries in it that I want to show in the side menu, but unfortunately through this way the menu is only showing the last entry and only a single row.
I know this might be because it is overwriting entries on the first cell.
Please view the link given above and please help me out with this.
This code will solve your problem your are creating BtSimplemenuitem and BTSimplesidemmenu in every iteration so it will make a lot of sidemenu just change your code to this will work
-(void)show
{
sideMenu.delegate = self;
int count;
NSMutableArray *itemsArry = [[NSMutableArray alloc] init];
count = [rssOutputData count];
for (int i = 0; i < count; i++){
NSString *items = [[rssOutputData objectAtIndex:i]xmltitle];
BTSimpleMenuItem *item = [[BTSimpleMenuItem alloc]initWithTitle:[[rssOutputData objectAtIndex:i]xmltitle] image:[UIImage imageNamed:#"arrow.png"]
onCompletion:^(BOOL success, BTSimpleMenuItem *item) {
catLbl.text = [[rssOutputData objectAtIndex:i]xmltitle];
}];
[itemsArry addObject:item];
}
NSArray *itemSarry=[[NSArray alloc] initWithArray:itemsArry];
sideMenu = [[BTSimpleSideMenu alloc]initWithItem:itemSarry addToViewController:self];
[sideMenu toggleMenu];
}

Programmatically add UIPickerView and Fill its data from Sqlite

I have a UIPickerView control to fill it from sqlite.
this is my singleSelectedData property on .h file:
#property (nonatomic,strong)NSMutableArray *singleSelectedData;
this is my ViewDidload method:
- (void)viewDidLoad
{
[super viewDidLoad];
_singleSelectedData=[[NSMutableArray alloc] init];
[self loadQuestions];
}
this is the loadQuestions method which i create uipickerview. Also it loads many controls. Here you see only picker view section:
else if (inputType==SingleSelectionQuestion)
{
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,656,768,44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(pickerCancel:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone:)];
[barItems addObject:doneBtn];
pickerToolbar.hidden=YES;
myPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0,700,768,260)];
myPicker.tag = [[[dataQuestions objectAtIndex:k] valueForKey:#"ids"] integerValue];
myPicker.showsSelectionIndicator = TRUE;
myPicker.dataSource = self;
myPicker.delegate = self;
myPicker.hidden = YES;
[myPicker setBackgroundColor:[UIColor grayColor]];
myPicker.tag=[[[dataQuestions objectAtIndex:k] valueForKey:#"ids"] integerValue];
UIButton *buttonControl = [UIButton buttonWithType:UIButtonTypeCustom];
buttonControl.tag = [[[dataQuestions objectAtIndex:k] valueForKey:#"ids"] integerValue];
[buttonControl setBackgroundImage:[UIImage imageNamed:#"combobox.png"] forState:UIControlStateNormal];
[buttonControl setTitle:#"Seçiniz" forState:UIControlStateNormal];
[buttonControl setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
buttonControl.frame = CGRectMake(30,sectionY + mainCategoryHeight+subCategoryHeight+questionLabel.frame.size.height +offsetQuestion,433,38);
[buttonControl addTarget:self action:#selector(selectSingleChoice:) forControlEvents:UIControlEventTouchUpInside];
NSMutableArray *choices=[[NSMutableArray alloc] initWithArray:[[DBHelper getSharedInstance:[LoggedinUser sharedCenter].dbFileName] getQuestionChoicesByQuestionID:[[dataQuestions objectAtIndex:k] valueForKey:#"ids"]]];
_singleSelectedData= [choices valueForKey:#"choiceName"];
// singleSelectedData=[[NSMutableArray alloc] initWithObjects:#"aaa",#"vvv", nil];
[pickerToolbar setItems:barItems animated:YES];
[self.scrlview addSubview:pickerToolbar];
[self.scrlview addSubview:buttonControl];
}
EDIT:
this is the method i select from sqlite:
- (NSMutableArray*) getQuestionChoicesByQuestionID:(NSString *)questionID
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:#"select CHOICE_ID,QUESTION_ID,NAME FROM T_PROGRAM_QUESTION_CHOICE as p INNER JOIN T_QUESTION_OPTION_CHOICE as c on c.ID=p.CHOICE_ID WHERE QUESTION_ID=%#",questionID];
const char *query_stmt = [querySQL UTF8String];
NSMutableArray *questionIDs = [[NSMutableArray alloc]init];
NSMutableArray *choiceIDs = [[NSMutableArray alloc]init];
NSMutableArray *choiceNames = [[NSMutableArray alloc]init];
NSMutableArray *mainArray = [[NSMutableArray alloc]init];
if (sqlite3_prepare_v2(database,
query_stmt, -1, &statement,nil) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *choiceID = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)];
[choiceIDs addObject:choiceID];
NSString *questionID = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 1)];
[questionIDs addObject:questionID];
NSString *choiceName = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 2)];
[choiceNames addObject:choiceName];
}
sqlite3_reset(statement);
if(questionIDs !=nil)
{
for (int index = 0; index<questionIDs.count; index++) {
NSMutableDictionary *dataAddDic =[[NSMutableDictionary alloc]init];
[dataAddDic setValue:[choiceIDs objectAtIndex:index] forKeyPath:#"choiceID"];
[dataAddDic setValue:[questionIDs objectAtIndex:index] forKeyPath:#"questionID"];
[dataAddDic setValue:[choiceNames objectAtIndex:index] forKeyPath:#"choiceName"];
[mainArray addObject:dataAddDic];
}
if(mainArray.count>0)
{
return mainArray;
}
else
{
return nil;
}
}
else
{
NSLog(#"problem1: %#",statement);
return nil;
}
}
else
{
NSLog(#"problem2: %#",statement);
}
}
else
{
NSLog(#"problem3");
}
return nil;
}
as you see i set singleSelectedData to an array. THE ARRAY is NOT NULL. But somehow my picker view has no items when the view loads. if i set singleSelectedData array with objects manually. The items show. I don't understand what is the difference and wondering how can i get items from sqlite and show it?

How to create a Multidimensional Array using loop in IOS xcode

I would like to create an array for iOS platform like the PHP syntax below, many thanks ~~
$createArray = array ();
for ($i = 0; $i<10; $i++) {
$createArray[$i]['name'] = $name;
$createArray[$i]['age'] = $age;
}
Save your values in NSDictionary and add that dictionary into your array
NSMutableArray *theArray = [NSMutableArray array];
for (int indexValue = 0; indexValue<10; indexValue++) {
NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
[theDictionary setObject:name forKey:#"name"];
[theDictionary setObject:age forKey:#"age"];
[theArray addObject:theDictionary]
}
While Retrieving time,
NSString *name = [[theArray objectAtIndex:indexValue] objectForKey:#"name"];
NSString *age = [[theArray objectAtIndex:indexValue] objectForKey:#"age"];
you might find it helpful:
array = [[NSMutableArray alloc] init];
for (int i = 0; i < 8; i++) {
NSMutableArray *subArray = [[NSMutableArray alloc] init];
for (int j = 0; j < 8; j++) {
[subArray addObject:[NSNumber numberWithInt:0]];
}
[array addObject:subArray];
[subArray release];
}
also check this question
Try this.
array = [[NSMutableArray alloc] init];
for (int i = 0; i < 10; i++) {
NSMutableArray *subArray = [[NSMutableArray alloc] init];
for (int j = 0; j < 2; j++) {
//Do your Stuff
// [subArray addObject:name];
// [subArray addObject:Age];
}
[array addObject:subArray];
}
OR
Why can't try with the NSDictionary
You can use this. But this is not better way in IOS.
NSMutableArray *array[20];
for (int i=0;i< 20; i++)
{
array[i] = [NSMutableArray array];
for (int j=0;j<3;j++)
{
NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
[theDictionary setObject:name forKey:#"name"];
[theDictionary setObject:age forKey:#"age"];
[[array[i] addObject:theDictionary]
}
}
First you to have set An NSMutableDictionary on .h file
#interface MSRCommonLogic : NSObject
{
NSMutableDictionary *twoDimensionArray;
}
then have to use following functions in .m file
- (void)setValuesToArray :(int)rows cols:(int) col value:(id)value
{
if(!twoDimensionArray)
{
twoDimensionArray =[[NSMutableDictionary alloc]init];
}
NSString *strKey=[NSString stringWithFormat:#"%dVs%d",rows,col];
[twoDimensionArray setObject:value forKey:strKey];
}
- (id)getValueFromArray :(int)rows cols:(int) col
{
NSString *strKey=[NSString stringWithFormat:#"%dVs%d",rows,col];
return [twoDimensionArray valueForKey:strKey];
}

How to load a picker view by a for loop?

I want load my picker by a for loop.From 1 to 999.I loaded manually.
My code is here.How I use a "for loop" for load automaticaly. Thanks
- (void)numberWasSelected:(NSNumber *)selectedIndex element:(id)element;
#synthesize numbers = _numbers;
#synthesize selectedIndex = _selectedIndex;
self.numbers = [NSArray arrayWithObjects:#"1", #"2", #"3",............#"999" nil];
- (IBAction)selectNumbers:(UIControl *)sender {
[ActionSheetStringPicker showPickerWithTitle:#"Select a number !" rows:self.numbers
initialSelection:self.selectedIndex target:self
successAction:#selector(numberWasSelected:element:)
cancelAction:#selector(actionPickerCancelled:) origin:sender];
}
- (void)numberWasSelected:(NSNumber *)selectedIndex element:(id)element {
self.selectedIndex = [selectedIndex intValue];
self.numberTextField.text = [self.numbers objectAtIndex:self.selectedIndex];
}
NSMutableArray* array = [NSMutableArray new];
for (int i = 1; i <= 999; i++) {
[array addObject:[NSString stringWithFormat:#"%d", i]];
}
self.numbers = array;

Resources