I want to create uitableview like the following , also I need to add a third section that contains rows
any suggestion please
Do all of this in an UITableViewController with -
UITableViewDelegate, UIPickerViewDelegate, UITableViewDataSource, UIPickerViewDataSource -
For Patient & ACC => Set a UITextField as a Subview (Do this in the cellForRowAtIndexPath for their respective rows)
For Birth and Date => Use the UIDatePickerView and for row ==2 and row ==4 respectively.
For Modality => I do not know what you mean by that; But you need to do something similar
For getting another section for the other two values; Return Section = 2 in
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
and set title for those accordingly (if needed)
Code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.clearsOnBeginEditing = YES;
textField.textAlignment = UITextAlignmentRight;
textField.returnKeyType = UIReturnKeyDone;
textField.delegate = self;
[cell.contentView addSubview:textField];
if (indexPath.row == 2)
{
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.datePickerMode = UIDatePickerModeDate;
[datePicker addTarget:self action:#selector(datePickerValueChangedd:) forControlEvents:UIControlEventValueChanged];
datePicker.tag = indexPath.row;
textField.inputView = datePicker;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
NSString *local;
local = [self datePickerValueChangedd: datePicker];
NSLog(#"%#", local);
textField.text =local;
[datePicker reloadInputViews];
[datePicker release];
}
}
// Configure the cell...
NSInteger row = [indexPath row];
cell.textLabel.text = [doseInfoDetailsArray objectAtIndex:row];
return cell;
}
You can take UITableview with style "Group" and can have 3 sections.
Related
I have not found any good resource on here so I thought it would be great to get this up and out there... and stop me from pulling my hair out.
Acting like the app "Agenda" (calendar app) - I have a tableview with each cell representing a day. The next step is to place the eventsArray inside the appropriate cell - through cellForRowAtIndexPath.
I am only familiar with displaying the eventsArray with sections and rows - using each indexPath to represent an event... not an array of events. (Below I posted working code for others looking for help in that department, and as a reference).
It would be great if someone could put up some code to execute the eventsArray inside a single cell - I'm assuming it is a use of the cellForRowAtIndexPath I am not familiar with. Thank you in advance. Any questions just shoot.
{
#pragma mark - TableView stuff
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
A1Section *a1section = [self.sections objectAtIndex:section];
return [a1section.rows count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.sections count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
A1Section *a1section = [self.sections objectAtIndex:section];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE, MMM d, yyyy"];
NSString *myDateString = [formatter stringFromDate:a1section.date];
return [NSString stringWithFormat:#"%#", myDateString];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 47;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 75;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UILabel *timeLabel = nil;
UILabel *nameLabel = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.backgroundColor = [UIColor redColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(11.0, 13.0, 55.0, 15.0)];
[timeLabel setTag:1];
[timeLabel setBackgroundColor:[UIColor clearColor]];
timeLabel.textAlignment = UITextAlignmentLeft;
[timeLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:14]];
[timeLabel setTextColor:[UIColor blackColor]];
[timeLabel setLineBreakMode:NSLineBreakByClipping];
[timeLabel setNumberOfLines:1];
[cell.contentView addSubview:timeLabel];
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(85.0, 11.0, 200.0, 20.0)]; //x = 80.0, width = 235
[nameLabel setTag:2];
[nameLabel setBackgroundColor:[UIColor clearColor]];
nameLabel.textAlignment = UITextAlignmentLeft;
[nameLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:17]];
[nameLabel setTextColor:[UIColor blackColor]];
[nameLabel setLineBreakMode:NSLineBreakByTruncatingTail];
[nameLabel setNumberOfLines:1];
[cell.contentView addSubview:nameLabel];
} else {
timeLabel = (UILabel *)[cell.contentView viewWithTag:1];
nameLabel = (UILabel *)[cell.contentView viewWithTag:2];
}
A1Section *a1section = [self.sections objectAtIndex:indexPath.section];
PFObject *object = [a1section.rows objectAtIndex:indexPath.row];
NSDate *someDate = [object objectForKey:#"startDate"];
NSString *time = [self.dateFormatter stringFromDate:someDate];
[(UILabel *)[cell.contentView viewWithTag:1] setText:time];
[(UILabel *)[cell.contentView viewWithTag:2] setText:[object objectForKey:#"textContent"]];
return cell;
}
}
I'm not entirely sure what you're asking, but I presume you're asking how to use an array's contents for display in cellForRowAtIndexPath. If so, you would just have an array property that contains the data you want. Then, in cellForRowAtIndexPath, you can do something like
NSString *name = [self.dataArray objectAtIndex:indexPath.row];
nameLabel.text = name;
Does that answer your question?
In the .h
#interface WordListTableController : UITableViewController <UITextFieldDelegate>
In the .m
// Customize the appearance of table view cells.
- (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] autorelease];
}
// Configure the cell...
UITextField *FirstField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 130, 25)];
FirstField.delegate = self;
FirstField.tag = indexPath.row;
[cell.contentView addSubview:FirstField];
FirstField.returnKeyType = UIReturnKeyNext;
[FirstField release];
return cell;
}
// Handle any actions, after the return/next/done button is pressed
- (BOOL)textfieldShouldReturn:(UITextField *)textfield
{
[textfield resignFirstResponder];
return YES;
}
What am I missing? Breakpoint is not being visited?
The way you are adding the UITextField is problematic. You add it each time cellForRowAtIndexPath is called without ever removing it. So you might actually end up with several text fields stacked on top of each other.
Try moving FirstField's creation in the if (cell == nil) {} block. Maybe this will also solve your problem.
you do not need repeat create it, just create it while the cell was created!
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
// Configure the cell...
UITextField *FirstField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 130, 25)];
FirstField.delegate = self;
FirstField.tag = indexPath.row;
[cell.contentView addSubview:FirstField];
FirstField.returnKeyType = UIReturnKeyNext;
[FirstField release];
}
Use this code snippet. textfieldShouldReturn is wrong.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
I added uitextfield programatically to uitableview cell, the problem is when I type on the textfield and click done or move from the textfield the keyboard doesn't disappear.
Any suggestion to solve that?
#interface test_20110605ViewController : UIViewController <UITextFieldDelegate> {
UITextField *PtienttextField ;
}
#property ( nonatomic , retain ) UITextField *PtienttextField ;
#end
at m file
#implementation test_20110605ViewController
#synthesize PtienttextField ;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.row == 0 && indexPath.section == 1 ) {
//cell.textLabel.text = "Patient Name";
UILabel *PatientLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 120, 25)];
PatientLabel.backgroundColor = [UIColor clearColor];
PatientLabel.tag = 1;
PatientLabel.text = #"Patient Name" ;
[cell.contentView addSubview:PatientLabel];
PtienttextField = [[UITextField alloc]initWithFrame:CGRectMake(140, 10, 400, 30)];
PtienttextField.clearsOnBeginEditing = YES;
PtienttextField.textAlignment = UITextAlignmentRight;
PtienttextField.returnKeyType = UIReturnKeyDone;
PtienttextField.delegate = self;
//PtienttextField.tag = 2 ;
[PtienttextField setBorderStyle:UITextBorderStyleBezel];
[cell.contentView addSubview:PtienttextField];
}
}
// Configure the cell.
return cell;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
return YES;
}
#end
I think you're missing the following in textFieldShouldReturn:
[textField resignFirstResponder];
then your return YES;
try putting a NSNotification when keyboard is appeared and set the textfield as firstresponsder. The problem you are using might be because of the textfield is not the first responsder in your case.
I want to know how to make text field in tableview in iphone.
it means how to add uitextfield on uiTableviewCell and How handle it?
In order to handle means respond to it delegate and fetching value at the time of submission....
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0,0,320,50)];
textField.tag = 1000;
[cell.contentView addSubview:textField];
[textField release];
In customization part,
UITextField *textField = [[cell contentView] viewWithTag:1000];
textField.text = #"Your text";
Tryout this code in CellForRowAtIndexPath:
UITextField *textField = [UITextField alloc] initWithFrame:CGRectMake(0,0,50,50)];
cell.contentView =textField;
[textField release];
Find the below link
Having a UITextField in a UITableViewCell
or try this one
- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:#"Cell"];
if( cell == nil)
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"] autorelease];
cell.textLabel.text = [[NSArray arrayWithObjects:#"First",#"Second",#"Third",#"Forth",#"Fifth",#"Sixth",#"Seventh",#"Eighth",#"Nineth",#"Tenth",nil]
objectAtIndex:indexPath.row];
if (indexPath.row % 2) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 21)];
textField.placeholder = #"Enter Text";
textField.text = [inputTexts objectAtIndex:indexPath.row/2];
textField.tag = indexPath.row/2;
textField.delegate = self;
cell.accessoryView = textField;
[textField release];
} else
cell.accessoryView = nil;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
Create a instance of either UITextView Or UTextField and add it into the UITableViewCell as sub view.
For my current project i need to have a table in which contains textfield in each cell, the number of cells and textfield must be dynamic, it's depends on the number of data in a MutuableArray. I have the textfield in cells working, but i can't get/set the textfield value. I wonder if you guys can help me out or at least correct me what I did wrong? Thank's alot in advance. See code snippets below:
// Adds textfield into cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSUInteger row = indexPath.row;
X10ArchiefIndexDefs *curIndex = [indexDefinities objectAtIndex:row];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
BOOL bShowSelection = ([curIndex.HasVasteWaarden isEqualToString:#"false"]);
if (bShowSelection) {
bShowSelection = !([curIndex.DataType isEqualToString:#"Datum"]);
}
if ([indexPath section] == 0) {
if (bShowSelection) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
UITextField *editField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
editField.adjustsFontSizeToFitWidth = YES;
editField.textColor = [UIColor blackColor];
editField.placeholder = curIndex.Naam;
editField.keyboardType = UIKeyboardTypeDefault;
editField.returnKeyType = UIReturnKeyNext;
editField.backgroundColor = [UIColor whiteColor];
editField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
editField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
editField.textAlignment = UITextAlignmentLeft;
editField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
editField.tag = [curIndex.UID intValue];
[editField setEnabled: YES];
[cell addSubview:editField];
[editField release];
}
}
return cell;
}
In some case i'm using popovercontroller to display list of data. User can select a value uit of the popup. This code is executed when there is a value selected:
- (void)selectedValue:(NSString *) value {
//---update value of the text field ---
//The first attempt it doesn't put the text to text field
//static NSString *CellIdentifier = #"Cell";
//UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//
//if (cell == nil) {
// cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//}
// second attempt it crashes
X10ArchiefIndexDefs *curIndex = [indexDefinities objectAtIndex:curRow.row];
int index = [curIndex.UID intValue];
UITextField *textField = (UITextField *) [curCell viewWithTag: index];
if (textField) {
[textField setText:value];
}
[textField release];
[self.popOverController dismissPopoverAnimated:YES];
}
When cell is selected I'm making sure that the cell is saved for use later.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
X10ArchiefIndexDefs *curIndex = [indexDefinities objectAtIndex:indexPath.row];
if (!curIndex) {
return;
}
curRow = indexPath; // saves the selected row
if ([curIndex.VasteWaarden count] > 0) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
curCell = cell; // saves the selected cell
CGRect frame = [cell.superview convertRect:cell.frame toView:self.view];
self.detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.delegate = self;
self.popOverController = [[[UIPopoverController alloc] initWithContentViewController:detailViewController] autorelease];
X10ArchiefIndexDefs *curIndex = [indexDefinities objectAtIndex:indexPath.row];
self.detailViewController.Values = curIndex.VasteWaarden;
[self.popOverController presentPopoverFromRect:frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}
Again thank's alot in advance.
Cheers,
Inoel
In the second code snippet you are releasing the textField. You shouldn't do this because you haven't retained it. Because viewWithTag: simple gets a reference to the text field it doesn't retain the textField. So you are releasing it more times that it has been retained, so the retainCount reaches 0 and the textfield is dealloced from memory. Then when you attempt it the second time there is no textfield in the memory.
Just remove the:
[textField release];
From the second code snippet. If you don't understand why, then read some articles about memory management (just google it). It takes some time to understand it fully, at least I know it took me a while :)