How to limit maximum number of NSInteger in iOS? - objective-c

I have a table to show searchData in iOS with UISearchBar.
When i search with keywords, i retrieved data into NSMutableArray.
There is alot of data in NSMutableArray.
I just want to show about 20 datas in tableView.
Normally i used data to show in tableview,
return [self.myArrayData count];
It's show entire data from NSMutableArray.
I want to limit data count to show in tableView.
How can i limit that data count?
Thanks for helping.

NSInteger count = [self.myArrayData count];
if (count > 20) return 20;
return count;

Related

Array Index in Descending Order

I am developing an camera app ,and showing the taken photos in thumbnail view with the numbers .i am displaying the numbers from array index value in ascending order.Like this.In this image numbers are in 1 , 2, 3, i want the numbers in 3 ,2 ,1
Now i want the numbers in descending order.Please help me to do this .my code is ..
NSString *the_index_path = [NSString stringWithFormat:#"%li", (long)indexPath.row+1];
Cell.waterMark_lbl.text = the_index_path;
this is code is in cellforitem at index method in collection view .
Thanks in advance !!!
You can rearrange the data model in reverse order and from there the data will be displayed.
Plan B, more convoluted, it would not change the order. When you have to show the cell 0, display the last, with cell 1 shows the penultimate, etc. You should generate a new indexPath every time you have to show the data.
The other option I understand is that for the first cell you want to show the last number. The operation would be similar, but without generating the new indexPath.
You have to do the subtraction of the total number of elements. Suppose the total number of elements is NUMBER_OF_ELEMENTS
Cell.waterMark_lbl.text = [NSString stringWithFormat:#"%li",NUMBER_OF_ELEMENTS - indexPath.row ]
In the case of reorder the cells without reorder the data, you have to créate a new indexPath and get the data value for display
NSIndexPath *ip = [NSIndePath indexPathForRow:NUMBER_OF_ELEMENTS - indexPath.row inSection:indexPath.section ]
sorry for lexical errors. I have the objective c somewhat forgotten

Nsmutablearray insertobject atindex but move down already inserted objects

I wanted to ask if the following scenario is somehow possible to happen.
I have an Nsmutablearray and i have in it 5 objects. I load the Nsmutablearray on an uitableview. I want to insert a new object at the top of the other five objects but also move down all the previously inserted objects down by one. let me give you an example
0 one
1 two
2 three
3 four
4 five
i want to result like this after the new object inserted on the top
0 six
1 one
2 two
3 three
4 four
5 five
Any help appreciated.
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index
This method of NSMutableArray is what you are looking for.
If index is already occupied, the objects at index and beyond are shifted by adding 1 to their indices to make room.
The method you are looking for is: - [NSMutableArray insertObject:atIndex:].
In your example, the index would be 0.

How do I get the number of Spaces in OSX?

I want the number of Spaces in OSX snow leopard.
If I use 4 spaces, I want that number 4!
I can't find a function or a class related with that.
Information about Spaces is stored in Dock preferences. You can access the preferences via NSUserDefaults to get the number of rows and columns, and the number of spaces is the product of those:
NSDictionary *dockPrefs = [[NSUserDefaults standardUserDefaults]
persistentDomainForName:#"com.apple.dock"];
int rows = [[dockPrefs objectForKey:#"workspaces-rows"] intValue];
int cols = [[dockPrefs objectForKey:#"workspaces-cols"] intValue];
int nspaces = rows * cols;

How do I get the section index for a row index in objective-c and UITableView

I want to select a given row in the UTTableView from a index but need to know also the section index.
int localCurrentIndexPath = 9;
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:localCurrentIndexPath inSection:??];
I need fill in the inSection part. Do I need to supply the inSection value?
Yes. If you only have 1 section, then use 0.

Comparing values In 2 different NSArray objects

I have 2 NSArray's that are holding values...
For example NSArray 1 has values 1 2 4 in it
and NSArray 2 has values 1 2 4 5 6 in it.
How can I write code to compare these 2 arrays to get the following information...
Count the values that are the same (so in this case 3) and count the values that are not the same (in this case 2).
I am simply populating the arrays like this:
NSString *s = #"1,2,4";
NSArray *numbers = [s componentsSeparatedByString:#","];
where *s is actually getting the text from a UITextField. If sorting mattering in comparing can you show me code to sort to make sure the user doesnt put the numbers in order?
If you are fine with sets instead of arrays, you can use NSMutableSet instead of NSArray. NSMutableSet has nice methods like intersectSet: and minusSet:
I would probably use the following method of the NSArray class:
enumerateObjectsUsingBlock.
and code the block testing for membership in the other array with the method:
indexOfObjectIdenticalTo.
If this isn't clear to you let me know.