iphone, UITextField in UITableView - xcode

I'm trying to add textfields in a tableview. I want a label and a textfield in every row except the last row. I want a switch at the last row.
The problem is that the text fields are overlapping on the rest of the rows. I moved my textfield code inside the if(cell == nil) but that didn't work... here's my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = #"mainMenuIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
[cell setSelectedBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"highlightstrip.png"]]];
if (tableView.tag == 1) {
UILabel *lblName = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 90, 20)];
lblName.textAlignment = UITextAlignmentLeft;
lblName.font = [UIFont boldSystemFontOfSize:14];
lblName.backgroundColor = [UIColor clearColor];
lblName.tag = 31;
[cell.contentView addSubview:lblName];
[lblName release];
}
}
if (tableView.tag == 1) {
[(UILabel *) [cell viewWithTag:31] setText:[tableElements objectAtIndex:indexPath.row]];
// check if the last row
if (indexPath.row == 10) {
newsSwtich = [[[UISwitch alloc] initWithFrame:CGRectZero] autorelease];
[newsSwtich addTarget:self action:#selector(switchToggled:) forControlEvents: UIControlEventTouchUpInside];
cell.accessoryView = newsSwtich;
}
else {
UITextField *tempTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 10, 200, 20)];
tempTextField.delegate = self;
// tempTextField.placeholder = [tableElements objectAtIndex:indexPath.row];
tempTextField.font = [UIFont fontWithName:#"Arial" size:14];
tempTextField.textAlignment = UITextAlignmentLeft;
tempTextField.tag = indexPath.row;
tempTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
tempTextField.keyboardType = UIKeyboardTypeDefault; // type of the keyboard
tempTextField.returnKeyType = UIReturnKeyDone; // type of the return key
tempTextField.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
[cell.contentView addSubview:tempTextField];
[tempTextField release];
cell.accessoryView = UITableViewCellAccessoryNone;
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
When I scroll up and down the text fields are overlapped , I mean after I enter the text in the first row and scroll down, I can see that textfield copied at the last row as well.

Cell reuse is causing the text fields to overlap. Each time the cell is being reused, you are adding a text field or a switch. These are piling up. You will need to remove the older subview which would either be the switch or the text field before adding one.

why you are using two tableviews just do this
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.row==[tableviewarray count]){
//dont add label or textfield
}
else{
// add label or textfield
}
}

Related

Button pressed in Table View Cell with reusable cells

I'm struggling with the following problem.
I've got a table view of options for a boat. Each option (cell) has a name, a price and a button that indicates whether the option has been selected or not (see attached screenshot)-
The problem is that when I tap on a couple of options from Section 1, I get the button_pressed state also for cells in the last section. Probably this happens because cells are reused ?
This is the function that gets called when the cell is tapped, to add the option.
- (IBAction)cellButtonSelection:(id)sender {
float prezzoParziale;
UIButton * button = sender;
//NSLog(#"%#",button.superview.superview);
if ([button.superview.superview isKindOfClass:[CellOptions class]] ) {
CellOptions * cellOpt =(CellOptions *)button.superview.superview;
//BOOL selectionState = NO;
cellOpt.selected =! cellOpt.selected;
if(cellOpt.selected){
//UIButton *button = cellOpt.button1;
[button setImage:[UIImage imageNamed:#"checkedButton.png"] forState:UIControlStateNormal];
prezzoParziale = [cellOpt.label2.text floatValue];
[accessoriSelezionati addObject:cellOpt.label1.text];
[accessoriSelezionatiPrezzi addObject: cellOpt.label2.text];
NSLog(#"stampa: %#", accessoriSelezionati);
}else{
//UIButton *button = cellOpt.button1;
[button setImage:[UIImage imageNamed:#"uncheckedButton.png"] forState:UIControlStateNormal];
prezzoParziale = [cellOpt.label2.text floatValue] * -1;
[accessoriSelezionati removeObject:cellOpt.label1.text];
[accessoriSelezionatiPrezzi removeObject: cellOpt.label2.text];
}
[self updateTotal:prezzoParziale]; }
And.....
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CellOptions * cellOpt;
cellOpt = [tableView dequeueReusableCellWithIdentifier:CellOption forIndexPath:indexPath];
cellOpt.label1.text = [[[accessoriXtipologia objectForKey:tipologia] objectAtIndex:indexPath.row] valueForKey:#"nome"];
cellOpt.label2.text = [[[[accessoriXtipologia objectForKey:tipologia] objectAtIndex:indexPath.row] valueForKey:#"prezzo"] stringValue];
return cellOpt;
}
EDIT: Now it's working
Added in cellForRowAtIndexPath:
cellOpt.selected = [accessoriSelezionati containsObject:cellOpt.label1.text];
if (cellOpt.selected) {
[cellOpt.button1 setImage:[UIImage imageNamed:#"checkedButton.png"] forState:UIControlStateNormal];
}
else {
[cellOpt.button1 setImage:[UIImage imageNamed:#"uncheckedButton.png"] forState:UIControlStateNormal];
}
return cellOpt;
Yes, this is due to cell reuse. In cellForRowAtIndexPath, in addition to setting the labels, you've got to set the selected state by doing something like this:
cellOpt.selected = [accessoriSelezionati containsObject:cellOpt.label1.text];

How to center text in a UITableViewCell when accessory is UITableViewCellAccessoryCheckmark

I'm trying to keep text centred in a UITableViewCell when I have the accessoryView set to UITableViewCellAccessoryCheckmark. I'm also using a storyboard with this class.
Here's a screenshot of what I don't want.
How do I stop the text from being indented to the left?
My willDisplayCell method..
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == [self cellToCheckmark]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
NSLog(#"Not Being Checked");
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.textLabel.textAlignment = UITextAlignmentCenter;
if (cell.shouldIndentWhileEditing == YES) {
cell.shouldIndentWhileEditing = NO;
}
}
CellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
static NSString *CellIdentifier = #"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.textAlignment = UITextAlignmentCenter;
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"Google";
break;
case 1:
cell.textLabel.text = #"Bing";
break;
case 2:
cell.textLabel.text = #"Yahoo!";
break;
default:
break;
}
}
if (indexPath.row == [self cellToCheckmark]) {
NSLog(#"Being Checked");
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
NSLog(#"Not Being Checked");
cell.accessoryType = UITableViewCellAccessoryNone;
}
if (cell.shouldIndentWhileEditing == YES) {
cell.shouldIndentWhileEditing = NO;
}
return cell;
}
There are 2 things you need to do:
First, you need to make sure you set the table style to Default: UITableViewCellStyleDefault. All other styles use a detailTextLabel in one way or another and you won't be able to set the textLabel's alignment property.
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
Then you can set the alignment of your cell's textLabel:
cell.textLabel.textAlignment = NSTextAlignmentCenter;
Then setting the accessory to checkmark based on whatever your data requires.
cell.accessoryType = ( myDataMatches ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone );
Screenshot
iOS 8+ solution:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.textLabel.text = ...;
if (/* your condition */) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.layoutMargins = UIEdgeInsetsMake(0, 40, 0, 10);
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
cell.layoutMargins = UIEdgeInsetsMake(0, 10, 0, 10);
}
return cell;
}
There seems to be a much simpler answer (See this answer). In short, you are centering the label in the cell's content view. Instead, center it in the cell itself, then it won't move.
Tried it out, it works on iOS 11.2.
These solutions work for default cells. If you have your own custom cell, the easiest way to do it is make the label have constraints both to the left margin and the right margin, then create an outlet for your left margin constraint, and set its constant in the cellForRowAtIndexPath method:
cell.accessoryType = isChosen ? .checkmark : .none
cell.leftMarginConstraint.constant = isChosen ? 40 : 0
This way, the right margin which is added for your accessoryType is then added to the left as well.
Don't know if the problem is solve
if (/* your condition */) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.indentationLevel = 4;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.indentationLevel = 0;
}

Xcode: Why does my UITextField act slow in my UITableViewCell?

I have an app where I load a UIViewController with an UITableView. In the UITableViewCells I have a UITextField. Which I have added in the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
if ([indexPath section] == 0) {
// Setting the cell textLabel.
[[cell textLabel] setText:[Customize objectAtIndex:indexPath.row]];
if ([indexPath row] == 0) { // Text
// Adding and customizing UITextField.
TextFieldText = [[UITextField alloc] initWithFrame:CGRectMake(160, 10, 140, 30)];
TextFieldText.placeholder = #"Random";
TextFieldText.autocorrectionType = UITextAutocorrectionTypeNo;
TextFieldText.autocapitalizationType = UITextAutocapitalizationTypeNone;
TextFieldText.returnKeyType = UIReturnKeyDone;
[cell addSubview:TextFieldText];
and so on.
The problem is that when you type in it, it is a little slow. When a letter/symbol is typed it appears in the UITextField and for a very short second the marker is still on the left side of the letter/symbol and slide across the letter/symbol. The keyboard also appears kind of slow.
Any thoughts?
Thanks in advance :)

xcode uitableview cell image align

I'm sure this is very simple but i can't find any documentation about it..
in my uitableview i have the text labels aligned to the right (my app is in heberw which is rtl)
i'm trying to add an image to the cell to the RIGHT of the text label, no luck, so far the image is aligned to the left and i can't find a way to align it to the right side of the cell.
here's my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.imageView.image = [UIImage imageNamed:#"tableicon.png"];
NSString *cellValue = [[stories objectAtIndex:indexPath.row] objectForKey:#"ArticleTitle"];
cell.textLabel.text = cellValue;
cell.textLabel.textAlignment = UITextAlignmentRight;
cell.textLabel.font = [UIFont systemFontOfSize:17];
cell.textLabel.numberOfLines = 2;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
Instead of adding the image as a subview of your UITableViewCell, add a container with right aligned subviews.
UIImageView *pic = myImageView;
UIView *picContainer = [[UIView alloc] initWithFrame:cell.frame];
picContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
picContainer.contentMode = UIViewContentModeRight;
pic.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
pic.center = CGPointMake(picContainer.frame.size.width-70, picContainer.frame.size.height/2);
[picContainer addSubview:pic];
[cell.contentView addSubview:picContainer];
This has the added benefit of automatically readjusting when the orientation changes.
try this one
cell.accessoryView=[UIImage imageNamed:#"tableicon.png"];

all images in table view are the same in iPhone app

I have a table view where I want a different image for each cell based on logic. Here is my cellforrowatindexpath code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:MyIdentifier] autorelease];
}
NSInteger *row = [indexPath row];
NSString *platform = [[myStringArray objectAtIndex: row];
cell.imageView.image = [self imageForInventory:platform];
return cell;
}
Here is my imageForInventory method:
- (UIImage *)imageForInventory:(NSString *)platform {
if (platform = #"Win") {
UIImage *winImage = [UIImage imageNamed:#"Vista.png"];
return winImage;
}else if (platform = #"Mac") {
UIImage *appleImage = [UIImage imageNamed:#"Apple.png"];
return appleImage;
}else if (platform = #"Linux") {
UIImage *linuxImage = [UIImage imageNamed:#"Linux.png"];
return linuxImage;
}
return nil;
}
This shows the winImage for every row ignoring the if statements. How do I get the image to set accordingly per cell?
It's probably your comparison code, to compare strings in imageForInventory use [str1 isEqualToString:str2]. You're using = which is the assignment operator right now, but even if you change it to == it wouldn't work.

Resources