I have a table wherein every row contains a textfield.After i entertext in all the rows which are text fields i want to retrieve the data into an array.Can any one tell me the idea of retrieving the data from every row which are textfields?
I cannot believe you. Bad grammar and no research.
To get an array of all text fields, I would do:
NSArray *subviews = self.view.subviews;
NSMutableArray *textFelds = [[NSMutableArray alloc]init];
NSMutableArray *indexPaths = [[NSMutableArray alloc]init];
NSObject *obj;
for (obj in subviews) {
if ([obj class] == [UITextField class]) {
[textFields addObject:obj];
CGPoint location = obj.center;
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
[indexPaths addObject:indexPath];
}
}
It first gets all of the subviews of the main view, then determines if they are text fields. If they are textFields, it adds that to an array and also gets the indexPath.
I hope I have answered your question
You are having the details like, which row has text fields, which are not.
You can get this feature by using two different king of cells
static NSString *CellIdentifierNormal = #"Cell";
static NSString *CellIdentifierTF = #"Cell";
UITableViewCell *cell;
if(indexPath.row %2 == 0){ //Example
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifierNormal];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifierNormal] autorelease];
}
} else {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifierTF] autorelease];
TF *tf = [[TF alloc] init];
tf.tag = 10;
}
In Text field row case, should not use the dequeueReusableCellWithIdentifier it re uses the existing cells, so that the text fields data may corrupt [This will happen when your data rows size greater than the table size - height]
Whenever you want collect the data from TF's
Just run loop with number of rows in table, and check same condition as you are using in creating cell.
Access the cell, and TF from cell by using tag number [10] from TF get the text
Related
I have a NSTableview which shows 50 rows currently which is fixed static. Now I want to add a feature into it. I want to add a more 50 rows to the table when user reach to the last row of the NSTableView. I mean currently I am showing fixed 50 rows in the application.
But now I want to add 50 more rows whenever user reaches to the last row of previous NsTableview.
Please give me any suggestions or any sample code to achieve my motive. Thanks in advance.
What i have done is(This code does not work with row selected(Down key pressed), and does not load new rows) -
-(void)awakeFromNib{
for(int i=0;i<25;i++)
[_list addObject:[[Person alloc] init]];
[_tableView reloadData];
[NSNotificationCenter.defaultCenter addObserver:self selector:#selector(scrollViewDidScroll:) name:NSViewBoundsDidChangeNotification object:[[_tableView enclosingScrollView] contentView]];
}
-(void) doSomething{
Person *p = [[Person alloc] init];
static int i =1;
NSString *string = [NSString stringWithFormat:#"John%i",i];
[p setName:string];
for(int i=0;i<25;i++)
[_list addObject:p];
// Need to use reloadDataForRowIndexes:columnIndexes: anyone please help me with this.
[_tableView reloadData];
i++;
}
- (void)scrollViewDidScroll:(NSNotification *)notification {
NSScrollView *scrollView = [notification object];
// Test if bottom of content view is reached.
CGFloat currentPosition = CGRectGetMaxY([scrollView visibleRect]);
CGFloat tableViewHeight = [self.tableView bounds].size.height;
if (currentPosition > tableViewHeight) {
// YOUR ACTION
[self doSomething];
}
I am trying to create a nested collection view. First I did for one level.
Created a data model class with String header. In app delegate created an array sectionTitle. Now in the nib, I added collection view & array controller and did all the bindings following this guide. Next in awakeFromNib I populated some random data
- (void)awakeFromNib {
int idx = 0;
NSMutableArray *sectionTitle = [[NSMutableArray alloc] init];
while (idx < 1) {
HeaderModel *header = [[HeaderModel alloc] init];
[header setHeader:[NSString stringWithFormat:#"Section %d", idx]];
[sectionTitle addObject:header];
idx++;
}
[self setHeaderData:sectionTitle];
}
Running it will give me 4 sections. I want to achieve similar layout as this. Section title, under it another collection of items. The answer given there only hints at using Nested collection view.
So I added another collection view in the first view prototype. Then I followed the same approach what I did for the first view(with different data model and array).
- (void)awakeFromNib {
int idx = 0;
NSMutableArray *sectionTitle = [[NSMutableArray alloc] init];
NSMutableArray *groupData = [[NSMutableArray alloc] init];
while (idx < 1) {
HeaderModel *header = [[HeaderModel alloc] init];
DataModel *name = [[DataModel alloc] init];
[header setHeader:[NSString stringWithFormat:#"Section %d", idx]];
[name setName:[NSString stringWithFormat:#"Name %d", idx]];
[sectionTitle addObject:header];
[groupData addObject:name];
idx++;
}
[self setHeaderData:sectionTitle];
[self setData:groupData]; //NSCollectionView item prototype must not be nil.
}
But now I get the error NSCollectionView item prototype must not be nil.
How do I resolve this ?
I have just answered a similar question here
But somehow by inserting the second NSCollectionView with I.B, you get a corrupted prototype for your inner NSCollectionViewItem. Simply try to extract each associated NSView into its own .xib
I currently have a number of arrays, each containing show title, description and duration. I have them in a further 'shows' array and I'm using this array to populate my NSTableView. What I would like to do is extract the show title from each of my arrays for the first column of my table, the description for the second and so on.
The code I have at the moment though takes the first array in my array of arrays and populates column one, the second array for the second column etc. How would I amend what I have so far to get the table to populate correctly? I've tried to use indexOfObject in place of objectAtIndex however doing so throws and exception. Here's my (simplified) code:
AppDelegate.m
- (void)applicationDidFinishLoading:(NSNotification *)aNotification
{
NSArray *show1 = [[NSArray alloc] initWithObjects:#"Title", #"A description", nil];
NSArray *show2...
NSArray *show3...
NSArray *show4...
self.array = [[NSMutableArray alloc] initWithObjects: show1, show2, show3, show4, nil];
[self.tableView reloadData];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
return [self.array count];
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSString *identifier = [tableColumn identifier];
if([identifier isEqualToString:#"title"]) {
NSTableCellView *title = [tableView makeViewWithIdentifier:#"title" owner:self];
title.textField.stringValue = [self.array objectAtIndex:0];
return title;
} else if {...}
return nil;
}
Michele Percich's comment is the correct answer: [self.array objectAtIndex:0] will return the first shows array. What you want is "NSArray * show = [self.array objectAtIndex:row]'" to get the show and then "[show objectAtIndex:0]" to get that shows title. Just a suggestion but I'd use an NSArray of NSDictionary's where the keys are the column identifiers. Then you could just use "[self.array objectAtIndex:row] valueForKey:identifier];"
Note also that the method you're overriding expects an instance of NSView (or subclass) to be returned (read the notes in the NSTableView.h header). You may want to use the tableView:objectValueForTableColumn:row: method instead and just return the appropriate NSString (based on the row & column identifier).
I'm trying to add some more cells to an existing UICollectionView, which is already filled with some cells.
I tried to use the CollectionView reloadData but it seems to reload the entire collectionView and I just wanted to add more cells.
Can anybody help me?
The UICollectionView class has methods to add/remove items. E.g., to insert an item at some index (in section 0), modify your model accordingly and then do:
int indexPath = [NSIndexPath indexPathForItem:index];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath inSection:0];
[collectionView insertItemsAtIndexPaths:indexPaths];
The view will do the rest.
The easiest way to insert new cells to the UICollectionView without having to reload all its cell is by using the performBatchUpdates, which can be done easily by following the steps below.
// Lets assume you have some data coming from a NSURLConnection
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *erro)
{
// Parse the data to Json
NSMutableArray *newJson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
// Variable used to say at which position you want to add the cells
int index;
// If you want to start adding before the previous content, like new Tweets on twitter
index = 0;
// If you want to start adding after the previous content, like reading older tweets on twitter
index = self.json.count;
// Create the indexes with a loop
NSMutableArray *indexes = [NSMutableArray array];
for (int i = index; i < json.count; i++)
{
[indexes addObject:[NSIndexPath indexPathForItem:i inSection:0]];
}
// Perform the updates
[self.collectionView performBatchUpdates:^{
//Insert the new data to your current data
[self.json addObjectsFromArray:newJson];
//Inser the new cells
[self.collectionView insertItemsAtIndexPaths:indexes];
} completion:nil];
}
When I hit the "+" button on a row in NSRuleEditor, a new row is created. How can I take influence on the criteria used for that row.
It seems NSRuleEditor defaults to selecting the first criterion sequentially from the list of possible values. I would much rather have the new row match the row where the "+" was clicked.
I was able to fake it by subclassing a private method:
- (void)_addOptionFromSlice:(id)slice ofRowType:(unsigned int)type
{
int rowIndex = [(NSRuleEditorViewSlice*)slice rowIndex];
NSArray *criteriaForRow = [self criteriaForRow:rowIndex];
NSArray *displayValuesForRow = [self displayValuesForRow:rowIndex];
self.template = [NSArray arrayWithObjects:criteriaForRow, displayValuesForRow, nil];
[super _addOptionFromSlice:slice ofRowType:type];
}
- (void)insertRowAtIndex:(NSInteger)rowIndex withType:(NSRuleEditorRowType)rowType asSubrowOfRow:(NSInteger)parentRow animate:(BOOL)shouldAnimate
{
[super insertRowAtIndex:rowIndex withType:rowType asSubrowOfRow:parentRow animate:shouldAnimate];
NSArray *template = self.template;
if (template != nil) {
[self setCriteria:[template objectAtIndex:0] andDisplayValues:[template objectAtIndex:1] forRowAtIndex:rowIndex];
}
}