I have UITableView that is created programatically, and I have written code to programmatically create a UITextField on each and every cell.
When the user taps on the text field, a picker appears and the user can select an item from the picker, and after the selection is made, the selection should be displayed in the text field of that particular cell.
Now the problem that I am facing is, whenever I tap on the text field from any of the table view cells, the picker appears correctly, but the after an item is picked from the picker, the value gets stored in text field of the last table view cell. Why does this happen?
Could someone please explain to me how to refer to a particular text field so that I can perform some action on that particular text field?
Create a subclass of UITableViewCell, and use it for cells. Add a property to reference its text field:
#interface YourTableViewCell : UITableViewCell
#property (strong) UITextField *textField;
#end
Then, assign the text view to the cell when you create it:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"cellIdentifier";
YourTableView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = ... // Create the cell here
UITextField *textField = ... // Create your text field here
cell.textField = textField; // Assign the text field to the cell
}
return cell;
}
Once you have done this, your text field is now linked to the cell it is in, so you can get the text view from the cell and set its value.
Related
I've created a UIButton dynamically in the table view cell. but there is a problem,- I'm not able to access the button or it's sender method.
When I click on a button it's giving the wrong tag or sometime it's not clickable. I've created button a with the help of for() loop in the tableview cell. I think the main problem is that I create the button in the for() loop. Can anyone help me to solve this issue?
My code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView *viewDisplaySize;
if (cell == nil)
{
NSString *myString =#"1111111:2222222:333333:44444:55555:6666:777777:888888888888:9999999999" ;
NSArray *myWords = [myString componentsSeparatedByString:#":"];
for (int i=0; i < [myWords count]; i++) {
//Create a button but can't able to get a correct tag
/// Or some time it's not click able
}
Whole code--- link of full code
The basic problem is that the way you're adding your subviews is not the best way. By adding the subviews inside a if(cell == nil) clause, and setting the tag value there, the tag value will never change. When the cell is reused, the tag will still be based on the row where it was created, not on where that reused cell now appears.
A better way to create your cell, would be to add any subviews you need in the init method of a custom cell class. This cleans up the code in cellForRowAtIndexPath so it only contains code you need to populate the cells. If the cell is made completely in code (no xib or storyboard for its view), then you should register the class (usually done in viewDidLoad of the table view controller) with registerClass:forCellReuseIdentifier:. Then, in cellForRowAtIndexPath there will be no need to check whether the cell is nil because it never will be. You still want to set the button's action and tag in cellForRowAtIndexPath, but now it will be reset for the proper row when the cell is reused.
I place textview in the prototype cells by storyboard and assign the textview's tag.
In the implantation method of
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:,
I wanna assign the textview another identifier so that I can obtain the textview by this identifier. The reason why I don't use the TAG property to do this is because that all the cells in my table view has the same prototype for reusing.
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
ETPost *post = postList[indexPath.row];
UITextView *textView = (UITextView*)[cell viewWithTag:TEXT_TAG];//TEXT_TAG 1000
textView.text =post.content;
return cell;
}
as you can see above, I use the viewWithTag, all the cells in my tableview have the same tag, so I have to another solution instead of assigning the indexPath to the TAG.
Alright, the answer is probably simpler than you think. But just subclass UITableViewCell and make sure that your table view is using your new subclass (you set this up in the prototype cell Custom Class in IB and StoryBoards). The only thing the subclass adds is a property that allows you to identify it.
#property short specialIdentifier;
Here's a more general link on UITableViewCells which I generally refer to when I need something done: cusomizing uitableviewcells
in -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath: you could assign the indexPath's row number to your textView's tag like:
cell.textView.tag = indexPath.row;
so this tag would correspond with your cell's indexPath distinct assuming you place all cells in ONE section.
I have a UITableView that I want to alter some of the static cells after I do other processing. I have outlets set up for the cells that I want to modify, but when I look at them using NSLog, they show nil, which indicates to me that I don't have the correct cell. For instance, in the image below I want to add the start time to the label just like I did for Date (date was done when creating the cells for which I got the current date),
I tap on the disclosure indicator which takes me to another scene (this was created in Storyboard, using segues to get from one scene to another) where I get the two times I need. I then return to the main scene (shown) and try to alter the Start Time label, but nothing happens. A NSLog of the label prior to trying to alter it returns this:
oStartTimeCell.textLabel.text: (null)
I have read in one of the Apple docs that this textfield is read-only. If that is true in this case, is there a way I can reload the cells with the updated information? Or is there another way to do this?
You're using the wrong approach. You should not create a reference to a cell using an outlet. Once the cell moves out of the visible view, the outlet will either be null or contain garbage data. Even if (in your situation) the cell will never move out of view, I think it shows you're trying to use a UITableView in a way that was not meant to be.
Instead put the data you want to display in your cells in a dataSource, e.g. an array.
The tableView should use the dataSource to configure the values displayed in the textLabels of the cells. Once you want to update the text displayed in the cells, change the values in the dataSource and call reloadData on the tableView to force the tableView to call -tableView:cellForRowAtIndexPath: and related UITableViewDataSource methods.
Try to create an IBOutlet for each cell and connect it:
IBOutlet UITableViewCell *cell1;
IBOutlet UITableViewCell *cell2;
IBOutlet UITableViewCell *cell3;
And also change your method to:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(indexPath.row == 0) return cell1;
if(indexPath.row == 1) return cell2;
if(indexPath.row == 2) return cell3;
if (cell == nil) {
//create cell;
}
return cell;
}
Are you using a UILabel to display the text ? . If you are just create an outlet to the UIlabel and update it any method like cellForRwoAtIndexPath or didSelectRowAtIndexPath etc that is called after you tableView is loaded.
If you are not using a UILabel and just using cell.textLabel you could do something like
cell.textLabel.text = #"ChangedText" in cellForRowAtIndexPathMethod. Make sure you are editing the required cell by checking indexPath.row
Do [tableView reloadData] to call cellForRowAtIndexPath.
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"searchCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
searchField = (UITextField *)[cell viewWithTag:10000];
[searchField resignFirstResponder];
}
Okay, I have a cell with a identifier name of "searchCell". This cell contains one single UITextField with tag 10000 that pops up a keyboard on the bottom of the screen. When a user touches another cell, the keyboard must be hidden so that the user can have larger space to scroll up and down.
However, when a keyboard has popped up and a user touches(select) a cell, the code above is called but not working... :( It seems like the assigned UITableViewCell is not the one that the user is currently using. What am I doing wrong here?
Make your class a delegate of UITextField
Go to the Storyboard file, click on the text field and go to connections inspector
Under outlets, connect the delegate to the View Controller
Run it in simulator. It will work
Is there a way to get or set the textLabel of a standard UITableViewCell inside a custom method? For instance of a pseudo code,
-(void)getTextLabelOfUITableViewCell
{
UILabel *tempLabel = [[UITableViewCell section:0 row:1] textLabel];
}
-(void)setTextLabelOfUITableViewCell:(UILabel *)data
{
[[UITableViewCell section:0 row:1] textLabel] = data;
}
I'm trying to bind this with the PickerView delegate methods so whenever I change values in the datepicker or picker, the selected UITableViewCell will reflect the changes.
Otherwise I'd have to create a custom UITableViewCell. It'd be nice to be able to programmatically use the standard UITableViewCell Styles.
Thanks in advance!
You should keep (or pass) a reference to the "currently selected cell". Then you can easily access its text label by simply writing:
// assuming the selected cell is kept in a property by name selectedTableViewCell
UILabel * tempLabel = self.selectedTableViewCell.textLabel;
In case you're wondering how to keep such a reference, you should implement the
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
method, and in it, simply state:
self.selectedTableViewCell = [tableView cellForRowAtIndexPath:indexPath];
This way, when the user taps the cell, it will be set as the "selected cell".
Another way to fly would be by adding the reference to the cell you want to edit the text from in the IBAction as a parameter, like this:
-(IBAction)getTextLabelOfUITableViewCell:(UITableViewCell *)editMe;
And then you could write something like:
UILabel * tempLabel = editMe.textLabel;
You pretty much can do whatever you want with the label from there on.
Hope this helps!