I'm Having a custom cell which is consist of 3 different textfield txtsq,txtPOB,txtRxunit , and this custom cell m taking in my table view Controller, in table view Controller class inside the tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath , now i want to set the 3 different range limit of my 3 different text field which is in custom cell but i fail to set range in my
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string{
NSString *resultStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (textField.tag ==SQ) {
if([self isNumeric:resultStr]){
if(resultStr.length <= 3){
if(resultStr.length >= 1){
NSString *firstLetter = [resultStr substringWithRange:NSMakeRange(0, 1)];
if(![firstLetter isEqualToString:#"0"]){
return YES;
}
}
else{
return YES;
}
}
}
}else
if([self isNumeric:resultStr]){
if(resultStr.length <= 5){
if(resultStr.length >= 1){
NSString *firstLetter = [resultStr substringWithRange:NSMakeRange(0, 1)];
if(![firstLetter isEqualToString:#"0"]){
return YES;
}
}
else{
return YES;
}
}
}
return NO;
}
I have already defined cell.txtSQ.tag=SQ;
cell.txtPOB.tag =POB;
cell.txtRxUnit.tag=RXunit;
After using this code my table view data is not persistent.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.skuNameArray.count > 0) {
OrderCell *cell = nil;
cell = (OrderCell *)[tableView dequeueReusableCellWithIdentifier:#"OrderCell"];
if(cell == nil){
cell = (OrderCell *)[OrderCell cellFromNibNamed:#"OrderCell"];
}
// Set Tag
cell.tag = indexPath.row;
cell.txtSQ.tag = indexPath.row;
cell.txtPOB.tag = indexPath.row;
cell.txtRxUnit.tag = indexPath.row;
// cell.txtSQ.tag=SQ;
//set Delegate
cell.txtPOB.delegate = self;
cell.txtRxUnit.delegate = self;
cell.txtSQ.delegate = self;
//set Values
cell.lblSKU.text = self.skuNameArray[indexPath.row];
cell.txtSQ.text = self.sqArray[indexPath.row];
cell.txtPOB.text = self.pobArray[indexPath.row];
cell.txtRxUnit.text = self.rxUnitArray[indexPath.row];
if (self.index == PCP || self.index == CURRENT_DAY_REPORTING_DETAIL) {
cell.txtSQ.enabled = NO;
cell.txtRxUnit.enabled = NO;
cell.txtPOB.enabled = NO;
}
[cell.txtSQ addTarget:self action:#selector(sqChanged:) forControlEvents: UIControlEventEditingChanged];
[cell.txtPOB addTarget:self action:#selector(pobChanged:) forControlEvents: UIControlEventEditingChanged];
[cell.txtRxUnit addTarget:self action:#selector(rxUnitChanged:) forControlEvents: UIControlEventEditingChanged];
cell.backgroundColor = [UIColor clearColor];
return cell;
}
Your SQ may be dynamic . So, make it static.
or
you might not have set delegate to self. Do this :
cell.txtSQ.delegate = self;
cell.txtPOB.delegate = self;
cell.txtRxUnit.delegate = self;
Set your tag carefully :
// Set Tag
cell.txtSQ.tag = SQ;
cell.txtPOB.tag = POB;
cell.txtRxUnit.tag = RXunit;
Related
i have a tableview which required three textview for description. i have made a prototype cell containing text view with tag 1000.
i want to reuse this textview but the text of the textview changes when i scroll.
Some time Faraz take position of haider textview and sometime zaidi take position of faraz.
i try to give them unique tag also but still it doesnot help me.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellViewIdentifier = nil;
long section = indexPath.section;
UITableViewCell *cell;
if(section == kSocialStatusSection) {
if(indexPath.row < socialResult.socialStatus.count){
cellViewIdentifier = kCheckBoxItemCellViewIdentifier;
cellViewIdentifier = [cellViewIdentifier stringByAppendingString:langAppendString];
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellViewIdentifier];
[cell setBackgroundColor:[UIColor clearColor]];
SocialStatus * socialStatus = [socialResult.socialStatus objectAtIndex:indexPath.row];
[cell setBackgroundColor:[UIColor clearColor]];
UILabel *titleLabel = (UILabel *) [cell viewWithTag:kTagTitleCellLabel];
// NSMutableDictionary *obj = [socialStatusArray objectAtIndex:indexPath.row];
[titleLabel setText:[LanguageUtilities isEnglishLanguage]?socialStatus.typeNameEn:socialStatus.typeNameAr];
UIButton *checkBox = (UIButton *) [cell viewWithTag:kTagCheckButton];
// checkBox.selected = NO;
if([socialStatus.selected boolValue]) {
checkBox.selected = YES;
}
else{
checkBox.selected = NO;
}
} else {
cellViewIdentifier = kDescriptionCellViewIdentifier;
cellViewIdentifier = [cellViewIdentifier stringByAppendingString:langAppendString];
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellViewIdentifier];
[cell setBackgroundColor:[UIColor clearColor]];
GCPlaceholderTextView *textArea = (GCPlaceholderTextView *)[cell viewWithTag:kTagDescriptionTextArea];
textArea.placeholder =getLocalizedString(#"Description");
textArea.delegate = self;
textArea.text = #"FARAZ";
}
}
else if(section == kSocialStatusProblems){
if(indexPath.row < socialResult.socialProblems.count){
cellViewIdentifier = kCheckBoxItemCellViewIdentifier;
cellViewIdentifier = [cellViewIdentifier stringByAppendingString:langAppendString];
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellViewIdentifier];
[cell setBackgroundColor:[UIColor clearColor]];
SocialProblem * socialProblem = [socialResult.socialProblems objectAtIndex:indexPath.row];
UILabel *titleLabel = (UILabel *) [cell viewWithTag:kTagTitleCellLabel];
// NSMutableDictionary *obj = [socialProblemsArray objectAtIndex:indexPath.row];
[titleLabel setText:[LanguageUtilities isEnglishLanguage]?socialProblem.typeNameEn:socialProblem.typeNameAr];
UIButton *checkBox = (UIButton *) [cell viewWithTag:kTagCheckButton];
if([socialProblem.selected boolValue]) {
checkBox.selected = YES;
}else{
checkBox.selected = NO;
}
} else {
cellViewIdentifier = kDescriptionCellViewIdentifier;
cellViewIdentifier = [cellViewIdentifier stringByAppendingString:langAppendString];
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellViewIdentifier];
[cell setBackgroundColor:[UIColor clearColor]];
GCPlaceholderTextView *textArea = (GCPlaceholderTextView *)[cell viewWithTag:kTagDescriptionTextArea];
textArea.placeholder =getLocalizedString(#"Description");
textArea.delegate = self;
textArea.text = #"Haider";
}
}
else if(section == kProblemTypesSection){
if(indexPath.row < socialResult.socialProblemsTypes.count){
cellViewIdentifier = kCheckBoxItemCellViewIdentifier;
cellViewIdentifier = [cellViewIdentifier stringByAppendingString:langAppendString];
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellViewIdentifier];
[cell setBackgroundColor:[UIColor clearColor]];
SocialProblemsType * socialProblemType = [socialResult.socialProblemsTypes objectAtIndex:indexPath.row];
UILabel *titleLabel = (UILabel *) [cell viewWithTag:kTagTitleCellLabel];
// NSMutableDictionary *obj = [problemTypeArray objectAtIndex:indexPath.row];
[titleLabel setText:[LanguageUtilities isEnglishLanguage]?socialProblemType.typeNameEn:socialProblemType.typeNameAr];
UIButton *checkBox = (UIButton *) [cell viewWithTag:kTagCheckButton];
if([socialProblemType.selected boolValue]) {
checkBox.selected = YES;
}else
{
checkBox.selected = NO;
}
} else {
cellViewIdentifier = kDescriptionCellViewIdentifier;
cellViewIdentifier = [cellViewIdentifier stringByAppendingString:langAppendString];
cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellViewIdentifier];
[cell setBackgroundColor:[UIColor clearColor]];
GCPlaceholderTextView * textArea= (GCPlaceholderTextView *)[cell viewWithTag:problemTypeTextViewTag];;
if (textArea != nil) {
textArea= (GCPlaceholderTextView *)[cell viewWithTag:problemTypeTextViewTag];
}else
{
textArea= (GCPlaceholderTextView *)[cell viewWithTag:kTagDescriptionTextArea];
[textArea setTag:problemTypeTextViewTag];
}
textArea.placeholder =getLocalizedString(#"Description");
textArea.delegate = self;
textArea.text = #"zaidi";
}
}
return cell;}
We can make do with an NSMutableArray object containing strings for each cell. You will have to fill the array with empty strings and use this info to fill the text field. It will be something like,
textView.text = [textViewStringsArr objectAtIndex:indexPath.row];
Since you're doing it programmatically within the view controller, you will have to set the view controller as your text view delegate. You can do
-(void)textViewDidEndEditing:(UITextView *)textView {
UITableViewCell *cell = (UITableViewCell*)textField.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
... Since you've the index path, you can map it to an array.
[textViewStringsArr replaceObjectAtIndexPath:indexPath withObject:textView.text];
}
This would be the basic idea. Start with a mutable array of empty strings and modify them every time the text view is updated by replacing the original text with the new text. Hope this helps.
I want to hide the NSArrays (menuItems, about, and charting) on the click for the specific section header for the tableview cell arrays. I got the section header to highlight and de-highlight depending on tap gesture recognizer count but I can not get the tableview cells to hide when that specific section header is clicked. Can someone please help? Thank you! Here is my .m code. My GCF float method is located at the bottom of the .m.
#interface SidebarTableViewController ()
#end
#implementation SidebarTableViewController {
NSArray *menuItems;
NSArray *about;
NSArray *charting;
}
- (void)viewDidLoad {
[super viewDidLoad];
menuItems = #[#"Home",#"Field Goal", #"Punt", #"Kick Off", #"Snapper", #"Punter"];
about = #[#"Home",#"About Us", #"Tutorial"];
charting = #[#"Home",#"Charting"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
if (indexPath.section==0) {
NSString *CellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
else if(indexPath.section==1) {
NSString *CellIdentifier2 = [charting objectAtIndex:indexPath.row];
UITableViewCell *cell2 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier2 forIndexPath:indexPath];
return cell2;
}
else {
NSString *CellIdentifier1 = [about objectAtIndex:indexPath.row];
UITableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1 forIndexPath:indexPath];
return cell1;
}
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Set the title of navigation bar by using the menu items
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
destViewController.title = [[menuItems objectAtIndex:indexPath.row] capitalizedString];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section==0)
{
return [menuItems count];
}
else if(section==1)
{
return [charting count];
}
else{
return [about count];
}
}
- (UIView*) tableView: (UITableView*) tableView viewForHeaderInSection: (NSInteger) section
{
UILabel *headerLabel = [[UILabel alloc]init];
headerLabel.tag = section;
headerLabel.userInteractionEnabled = YES;
headerLabel.backgroundColor = [UIColor grayColor];
if(section == 0){
headerLabel.text = [NSString stringWithFormat:#"Timers Without Charting"];
}
else if(section==1)
{
headerLabel.text = [NSString stringWithFormat:#"Charting with Timers"];
}
else{
headerLabel.text = [NSString stringWithFormat:#"About Us/Tutorial"];
}
headerLabel.frame = CGRectMake(0, 0, tableView.tableHeaderView.frame.size.width, tableView.tableHeaderView.frame.size.height);
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(catchHeaderGesture:)];
tapGesture.cancelsTouchesInView = NO;
[headerLabel addGestureRecognizer:tapGesture];
return headerLabel;
//return nil;
}
-(void)catchHeaderGesture:(UIGestureRecognizer*)sender
{
border ++;
if (border == 1)
{
UILabel *caughtLabel = (UILabel*)sender.view;
caughtLabel.layer.borderColor = [UIColor yellowColor].CGColor;
caughtLabel.layer.borderWidth = 2;
}
if (border == 2 )
{
UILabel *caughtLabel = (UILabel*)sender.view;
caughtLabel.layer.borderColor = [UIColor clearColor].CGColor;
caughtLabel.layer.borderWidth = 2;
border = 0;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
border ++;
if (border == 1)
{
UITableViewCell* cell = [menuItems objectAtIndex:indexPath.row];
cell.hidden = YES;
return 40.0;
}
if (border == 2 )
{ border = 0;
UITableViewCell* cell = [menuItems objectAtIndex:indexPath.row];
cell.hidden = YES;
}
return 0;
}
#end
You need a few couple things to make this work.
section headers that respond to taps.
a method for expanding or collapsing a section.
some way to track which sections are collapsed.
The first is trivial. Return a UIView for the header, and attach a UITapGestureRecognizer to it. You'll need a method to figure out which section it is. You can use the tag property, or you can store the views in an NSMutableArray.
In tableView:numberOfRowsInSection: you return 0 if the section is collapsed, or the actual number, if not.
In the handler for the gesture recognizer, you toggle the collapsed/expanded state, and then you call `[self.tableView reloadSections:withRowAnimation:] to update the visuals.
(I do see in your posted code that you already handle part of this.)
After i updated XCode to 7.0 version
all my UITableViews view with loading image using SDWebImage component scroll with lag
This is my cellForRow method
and when i delete line codes with loading image it scroll much faster and smooth
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MatchTodayCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"matchCell" forIndexPath:indexPath];
MatchTodayObject *obj = [[self.matchesArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.hostLabel.text = obj.hostTeam;
cell.guestLabel.text = obj.guestTeam;
cell.timeLabel.text = [NSString stringWithFormat:#"%# %#", [self checkDate:obj.date],obj.time ];
[cell.hostImg sd_setImageWithURL:[NSURL URLWithString:obj.hostLogo] placeholderImage:IMG(#"teamlogoplaceholder")];
[cell.guestImg sd_setImageWithURL:[NSURL URLWithString:obj.guestLogo] placeholderImage:IMG(#"teamlogoplaceholder")];
cell.notificationButton.tag = obj.ID ;
if ([self.matchIdsWithNotification containsObject:[NSNumber numberWithInteger: obj.ID ]]) {
[cell.notificationButton setImage:IMG(#"NotificationsActivated") forState:UIControlStateNormal];
cell.notificationButton.userInteractionEnabled = NO;
}
else
{
[cell.notificationButton setImage:IMG(#"NotificationsnotActivated") forState:UIControlStateNormal];
cell.notificationButton.userInteractionEnabled = YES;
}
cell.hostPointLabel.text = [NSString stringWithFormat:#"%ld", obj.hostPoint ];
cell.guesPointlabel.text = [NSString stringWithFormat:#"%ld", obj.guestPoint ] ;
if (obj.isFinished) {
cell.stateImg.image = IMG(#"endedtag");
cell.notificationButton.enabled = NO;
}
else
{
if (obj.isStarted) {
cell.stateImg.image = IMG(#"livetag");
cell.notificationButton.enabled = YES;
}
else{
cell.stateImg.image = IMG(#"Soontag");
cell.notificationButton.enabled = YES;
}
}
return cell;
}
In my project I need to implement the UITableview with some of the tableView cells are expandable and some of them are independent. If it is expandable cell need to indicate the '+' symbol.enter image description here. Can any one please help me out
I have created a small demo,
https://github.com/haripalwagh/ExpandableTableviewCell
Screenshot 1 : Before expanding a cell
Screenshot 2 : After expanding a cell
#interface ViewController ()
<UITableViewDataSource,
UITableViewDelegate>
{
UITableView *tblView;
NSArray *cell0SubMenuItemsArray;
BOOL isSection0Cell0Expanded;
}
#end
#implementation ViewController
# pragma mark - View Life Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
tblView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
tblView.backgroundColor = [UIColor clearColor];
tblView.delegate = self;
tblView.dataSource = self;
tblView.allowsSelection = YES;
tblView.scrollEnabled = YES;
tblView.alwaysBounceVertical = YES;
[self.view addSubview:tblView];
cell0SubMenuItemsArray = #[#"First Static Menu Item", #"Second Static Menu Item", #"Third Static Menu Item"];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.view setNeedsLayout];
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
[self updateViewDimensions];
}
- (void)updateViewDimensions
{
tblView.frame = CGRectMake(0, 40, 320, 550);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
# pragma mark - UITableView Delegate and Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
{
int cellCount = 2; // Default count - if not a single cell is expanded
if (isSection0Cell0Expanded)
{
cellCount += [cell0SubMenuItemsArray count];
}
return cellCount;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *strCellId = #"CellId";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strCellId];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (indexPath.section == 0)
{
if (indexPath.row == 0)
{
cell.textLabel.text = #"Expandable Cell";
UIImageView *accessoryImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
if (isSection0Cell0Expanded) // Set accessory view according to cell state - EXPANDED / NOT EXPANDED
{
accessoryImageView.image = [UIImage imageNamed:#"Minus.png"];
cell.detailTextLabel.text = #"Status : Expanded";
}
else
{
accessoryImageView.image = [UIImage imageNamed:#"Plus.png"];
cell.detailTextLabel.text = #"Status : Not Expanded";
}
cell.accessoryView = accessoryImageView;
}
else
{
if (isSection0Cell0Expanded && [cell0SubMenuItemsArray count] >= indexPath.row) // Check Expanded status and do the necessary changes
{
cell.textLabel.text = [NSString stringWithFormat:#"%#", [cell0SubMenuItemsArray objectAtIndex:indexPath.row - 1]];
}
else
{
cell.textLabel.text = #"Static Cell";
}
}
}
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
// Change status of a cell reload table
isSection0Cell0Expanded = !isSection0Cell0Expanded;
[tblView reloadData];
}
}
You have to manage like this for every expandable cell.
Hope this will help you..
Try this control: https://github.com/jonasman/JNExpandableTableView
It supports what you say. Tapping on a cell expands it.
I've got 3 table cells and somehow it displays the two last contents in the last cell. I did something wrong somewhere in my code, but I don't know where since I just follow a tutorial and try to do the same like in the tutorial, but instead of 2 cells I want 3 editable cells.
the full code:
#import "LocationAddViewController.h"
#import "Location.h"
#interface LocationAddViewController ()
- (void)prepareCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath;
- (UIBarButtonItem *)newCancelButton;
- (UIBarButtonItem *)newSaveButton;
- (UITextField *)newTextField;
#end
#implementation LocationAddViewController
#synthesize location;
#synthesize titleField;
#synthesize authorField;
#synthesize atextField;
#synthesize delegate;
- (void)dealloc {
[location release];
[titleField release];
[authorField release];
[atextField release];
[super dealloc];
}
- (id)initWithLocation:(Location *)aLocation andDelegate:(id)aDelegate {
if (self = [super initWithStyle:UITableViewStyleGrouped]) {
self.location = aLocation;
self.delegate = aDelegate;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.allowsSelection = NO;
titleField = [self newTextField];
titleField.keyboardType = UIKeyboardTypeASCIICapable;
[titleField becomeFirstResponder];
authorField = [self newTextField];
authorField.keyboardType = UIKeyboardTypeASCIICapable;
atextField = [self newTextField];
atextField.keyboardType = UIKeyboardTypeASCIICapable;
if (location.onelocationId) {
titleField.text = location.title;
authorField.text = location.author;
atextField.text = location.text;
} else {
titleField.placeholder = #"Title";
authorField.placeholder = #"Author";
atextField.placeholder = #"Text";
}
UIBarButtonItem *cancelButton = [self newCancelButton];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
UIBarButtonItem *saveButton = [self newSaveButton];
self.navigationItem.rightBarButtonItem = saveButton;
saveButton.enabled = NO;
[saveButton release];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (location.onelocationId) {
self.title = #"Edit Location";
} else {
self.title = #"Add Location";
}
}
-(IBAction)cancel {
[self.navigationController popViewControllerAnimated:YES];
}
-(IBAction)save {
location.title = titleField.text;
location.author = authorField.text;
location.text = atextField.text;
[self.delegate didChangeLocation:location];
[self.navigationController popViewControllerAnimated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell =
[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil] autorelease];
[self prepareCell:cell forIndexPath:indexPath];
return cell;
}
- (void)prepareCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
[cell.contentView addSubview:titleField];
} else {
[cell.contentView addSubview:authorField];
[cell.contentView addSubview:atextField];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
if (textField == titleField) {
[authorField becomeFirstResponder];
}
if (titleField == authorField) {
[self save];
}
return YES;
}
- (IBAction)textFieldChanged:(id)sender {
BOOL enableSaveButton =
([self.titleField.text length] > 0) && ([self.authorField.text length] > 0) && ([self.atextField.text length] > 0);
[self.navigationItem.rightBarButtonItem setEnabled:enableSaveButton];
}
- (UIBarButtonItem *)newCancelButton {
return [[UIBarButtonItem alloc]
initWithTitle:#"Cancel"
//auch im Original gelb
style:UIBarButtonSystemItemCancel
target:self
action:#selector(cancel)];
}
- (UIBarButtonItem *)newSaveButton {
return [[UIBarButtonItem alloc]
initWithTitle:#"Save"
//auch im Original gelb
style:UIBarButtonSystemItemSave
target:self
action:#selector(save)];
}
- (UITextField *)newTextField {
UITextField *textField =
[[UITextField alloc] initWithFrame:CGRectMake(10, 10, 285, 25)];
textField.font = [UIFont systemFontOfSize:16];
textField.delegate = self;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
[textField addTarget:self
action:#selector(textFieldChanged:)
forControlEvents:UIControlEventEditingChanged];
return textField;
}
#end
I suppose the problem is here:
- (void)prepareCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
[cell.contentView addSubview:titleField];
} else {
[cell.contentView addSubview:authorField];
[cell.contentView addSubview:atextField];
}
}
I'm not too much into the whole programming, but I guess I have to write 3 if-clauses? (Something like if (...) elsif (...) else (...)) Does anybody know it better than me?
In your prepareCell:forIndexPath, you are adding both subviews into the last cell. You can use an elseif just as your described so your method looks something like this
if (indexPath.row == 0) {
[cell.contentView addSubview:titleField];
} else if (indexPath.row == 1) {
[cell.contentView addSubview:authorField];
} else {
[cell.contentView addSubview:atextField];
}
You can add any amount of else ifs after the if.
You're right about the source of the error. You're adding authorField & atextField at the same coordinates. The correct way to include 3 causes for ifs is:
if (/* condition 1 */) {
}
else if ( /* condition 2 */) {
}
else if ( /* condition 3 */) {
}
Do this:
- (void)prepareCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
[cell.contentView addSubview:titleField];
} else if(indexPath.row == 1) {
[cell.contentView addSubview:authorField];
}else if(indexPath.row == 2) {
[cell.contentView addSubview:atextField];
}
}
if (indexPath.row == 0) {
[cell.contentView addSubview:titleField];
} else if(indexPath.row == 1) {
[cell.contentView addSubview:authorField];
}else if(indexPath.row == 2) {
[cell.contentView addSubview:atextField];
}