How can I update a tableview cell from a -(void) method? - ios

I am trying to update a specific dynamic cells font size from a -(void) method or some way without using cellForRowAtIndexPath. Is this possible?
Thanks

If you know the position of the index, as follows:
First create indexPath by row at position:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:0];
Then access to cell:
MyCell *cell = (MyCell *)[self.tableView cellForRowAtIndexPath:indexPath];

Call
[self.view.tableView reloadRowsAtIndexPaths:#[your_index_path] withRowAnimation:UITableViewRowAnimationAutomatic];
inside your method. that will cause row update

You can not update a cell without having a call to: cellForRowAtIndexPath.
When you want to update font size in a cell, then reload the cell. In your cellForRowAtIndexPath method add a conditional statement to check the indexPath of the cell you want a different font size and edit different font size. Do not forget to add else condition so that when the cell reloads it will make the font size normal for other cells.
However if you want somehow. Then you can use global reference variable to the cell. But this will only work if the cell is displaying on screen.
Here is the example code:
#interface TableViewController ()
{
UITableViewCell *aCell;
}
#end
#implementation TableViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *theCell = [tableView dequeueReusableCellWithIdentifier:#"TableViewCellIdentifier" forIndexPath:indexPath];
if(indexPath.row == SPECIFIC_ROW_NUMBER){
aCell = theCell;
theCell.tag = SPECIFIC_ROW_NUMBER;
}else if (theCell.tag == SPECIFIC_ROW_NUMBER){
// If the cell is dequeued
aCell = nil;
theCell.tag = 99999; // some unused row number
}
}
- (void)someMethod {
if(theCell != nil){
aCell.titleLabel.font = NEW_FONT;
}
}
#end

Related

How to add UISWitch to few rows in UITable in iOS?

I have a UITableView which contains so many rows.In my screen there can be UISwitch added for few rows in UITableView.Please tell me how can i do this?
Should i create a custom cell with a UISwitch & show or hide the UISwitch.
Should i add the UISwitch directly from the code in cellForRowAtIndexPath:.
Any suggestions please?
EDIT:
I have tried this code but this does not work.
SettingsCell *cell;
static NSString *CellIdentifier = #"SettingCell";
if (cell == nil)
{
cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleDefault];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
}
if(indexPath.row==2 || indexPath.row==3)
{
cell.switch_value.hidden=false;
}
else
{
cell.switch_value.hidden=true;
}
cell.label_text.text = [VALUES objectAtIndex:indexPath.row];
return cell;
If you are creating a new cell then you should use that cell for only required rows. For rest of the rows you should not take pain of hiding the switch.
If you create UISwitch directly from the code in CellForRowAtIndexPath, then you should take care while re-using the cell. If you use different identifier for both cell types, the show/hide problem will get resolved.
Both solutions work here : you can add an UISwitch in a UITableViewCell in cellForRowAtIndexPath: : be just careful to check that no previously created UISwitch exists before creating a new one.
The second solution consists in using a custom UITableViewCell subclass.
I prefer this solution because you don't have to cast your subviews or use viewWithTag: method, or even to check if your custom subview was already created.
You should create some simples subclasses, such as PickerCell, SwitchCell, TextFieldcell : thus you will have your own set of custom UITableViewCell subclasses ready to be used within all your projects.
Assuming you have registered your reuse identifier, try this :=
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SettingCellIdentifier = #"SettingCell";
static NSString *StandardCellIdentifier = #"StandardCell";
if (indexPath.row == 2 || indexPath.row == 3) {
SettingsCell *cell = (SettingsCell *)[tableView dequeueReusableCellWithIdentifier:SettingCellIdentifier forIndexPath:indexPath];
[cell setSelectionStyle:UITableViewCellSelectionStyleDefault];
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
cell.label_text.text = [VALUES objectAtIndex:indexPath.row];
// Do whatever you want with the switch here
return cell;
}
else
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:StandardCellIdentifier forIndexPath:indexPath];
// instantiate a regular UITableViewCell
cell.label_text.text = [VALUES objectAtIndex:indexPath.row];
return cell;
}
}

TableViewCell should not get reuse

I am using UITableview in my project, and also i have UITableviewcell for that UITableview. The problem is that cells are getting reused.if it get reused, when i select button i want the image to be changed for single cell and button isSelected =YES. So the button's selected image is getting reuse for every cell.
So,kindly help me out with this problem .
// In indexPathRow before return
cell button.tag=indexPath.row;
In buttonclick action
//
-(IBAction)BtnAction:(UIControl *)sender
{
UIButton *button = (UIButton*)sender;
NSIndexPath *buttonIP = [NSIndexPath indexPathForRow:sender.tag];
//Type cast it to CustomCell
CustomCell *cell = (CustomCell*)[table cellForRowAtIndexPath:buttonIP];
if(button.isSelected==NO)
{
cell.Imageview.image=selectedImage;
button.isSelected=YES;
}
else
{
cell.Imageview.image=OldImage;
button.isSelected=NO;
}
[table reloaddata];
}
insted of using the above code you can use this line of code
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(firstTime)
{
[tableView cellForRowAtIndexPath:indexPath].imageView.image = [UIImage imageNamed:#"selected image"];
[attatancearry addObject:value];
}
if(secondTime)
{
[tableView cellForRowAtIndexPath:indexPath].imageView.image = [UIImage imageNamed:#"absent image"];
[attatancearry removeObject:value];
}
}
and add the value to an another nsmutablearry as the attendence is added and when you press the same cell again you have to remove the value from the nsmutablearray you have added before, and reset the image back to absent. but you don't have to reload the entire tableview it will just change the cell which you have select right now. as your adding the value or index number of the cell into a array you can use the selection value for further use.
you have to see the cell which you have selected, and set a flag value as the selected index. and reload the table.
when you reload the table check the flag value and set the image which you want to set for the cell which is selected. it will give you different cell selection.
for example
int selectionFlag = -888;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectionFlag == indexPath.row)
{
//set the image you want to set for the cell which is selected.
}
else
{
//the normal image you want to set.
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectionFlag = indexPath.row;
[tableView realoadData];
}

Get UItextFields text from custom UITableView

I have a custom UITableViewCell with a UITextField (which is linked to the custom cells class). I am trying to access the textField from my VC class.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
menuCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if ([indexpath row] == 2) {
menuCell.nameTextField.delegate = self;
}
return cell;
}
-(void) textFieldDidEndEditing:(UITextField*) textfield
{
}
How do I get the textFields text from textFieldDidEndEditing?
Depending on where you want to access this text depends on how difficult it is.
Want to access the text in cellForRowAtIndex - (very easy)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
menuCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
if ([indexpath row] == 2) {
menuCell.nameTextField.delegate = self;
}
NSString * text = menuCell.nameTextField.text;
return cell;
If you want to access the text anywhere in the VC and the menuCell is unique (there is only one of them) - (medium difficult)
In your header file add the custom cell as a class
#class menuCell;
This means you can set it a variable in the interface
menuCell * _menuCell;
Next in cellForRowAtIndex you want to allocate this custom cell
- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == bCustomCellSection) {
if (!_menuCell) {
_menuCell = [tableView_ dequeueReusableCellWithIdentifier:bProfileNameCell];
_menuCell.nameTextField.delegate = self;
}
_menuCell.nameTextField.placeholder = #"Name";
_menuCell.selectionStyle = UITableViewCellSelectionStyleNone;
return _menuCell;
}
...
}
This means that we now have access to the menu cell from anywhere in the VC and can get the text by calling
_menuCell.nameTextField.text
Multiple custom cells with multiple textfields - (tough)
I have never done this but would probably do it one of two ways
a) Create an array and as we are creating the custom cells add a pointer to the textFields to the array each time. We can then access the textField we want from that array
For this method I would add the custom cells to a mutable array defined in the interface
NSMutableArray * cellsArray;
remember to initialise it in viewDidLoad
cellsArray = [NSMutableArray new];
Then in cellForRowAtIndex i would add the cell each time
menuCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
[cellsArray addObject: menuCell];
This obviously depends on how many sections we have. If we have more than one section it gets more complicated again:
Then we would need to add an array for each section to an overall array. This is quite complicated and could have a whole question on its own, there is a good link of how to do this here:
Once you have an array of cells (or an array of arrays of cells) you can call the cell you want based on the indexPath and get the textField
b) Call a pointer to the specific cell we want
menuCell * menuCell = [self tableView:table cellForRowAtIndexPath:indexPath];
and then get the textField from this cell as we did previously.
Remember you can calculate your own indexPath if you want to create one outside of cellForRow:
NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];
This method is pretty good if you want to access a specific cell but a bit cumbersome if you want to access it a lot and keep having to call this code all over your VC
Hope this helps
If you are asking how to get the text from the delegate method textFieldDidEndEditing, then you simply do this:
-(void) textFieldDidEndEditing:(UITextField*) textfield
{
NSString *textFieldText = textfield.text;
}
However, if you have multiple textFields and you want to know what textfield is calling the delegate, you could tag your textField:
[myTextField setTag:indexPath.row]
and then put a if statement in the delegate textFieldDidEndEditing like this:
-(void) textFieldDidEndEditing:(UITextField*) textfield
{
if(textfield.tag == index0) do something..
else if(textfield.tag == index1) do something..
}

Weird behaviour using 2 cell dentifiers in a tableViewController

I'm playing with the cell identifier in a TableviewController, I'm using storyBoard with dynamic cells.
I set 2 cells with 2 identifiers and used a custom row height:
In my viewDidLoad, I just insert some checking information to a mutableArray:
- (void)viewDidLoad
{
[super viewDidLoad];
_arrayCellContent = [[NSMutableArray alloc]init];
for (int i=0; i<15; i++) {
if (i%2 == 0) {
[_arrayCellContent addObject:#"white"];
}
else
[_arrayCellContent addObject:#"Brown"];
}
}
My cellForRow delegate method is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* identifierCellSlim = #"cellSlim";
static NSString* identifierCellFat = #"cellFat";
UITableViewCell *cellSlim = [tableView dequeueReusableCellWithIdentifier:identifierCellSlim forIndexPath:indexPath];
UITableViewCell *cellFat = [tableView dequeueReusableCellWithIdentifier:identifierCellFat forIndexPath:indexPath];
if (indexPath.row%2 ==0) {
cellSlim.textLabel.text = self.arrayCellContent[indexPath.row];
return cellSlim;
}
// Configure the cell...
else {
cellFat.textLabel.text = self.arrayCellContent[indexPath.row];
cellFat.detailTextLabel.text = #"yalla";
return cellFat;
}
}
The final outcome is:
Even cell row heights, not costumed. Why is that? (I know I might work it out with the right delegate method, but I just want to know why the IB not doing his job)
Also in the beginning the cells appear to be in the right color but when I click some of them they will transform to the other cell,
Does clicking the cell change the indexPath ?
Example:
You are dequeuing both cells in the same method call.
You should check for which cell is necessary for the given NSIndexPath and dequeue the relevant cell.

Editing UITableViewCell by tag

I have been trying to find a simple answer to something I think something that shouldn't be that difficult but I just either can't find a decent answer or can't wrap my head around it.
I am trying to change/edit the text label content of one specific tableview cell. So for examples sake I would like a UIButton outside of the UITableView but in the same View Controller to change the text within row 2, while keeping the others the same.
I am aware you use viewWithTag but other than that I cannot figure this out! Please help....
This is my code so far
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
EntryCell *cell = (EntryCell *)[contentTable dequeueReusableCellWithIdentifier:#"Cell"];
cell.mainLabel.tag = indexPath.row;
cell.mainLabel.text = #"hello";
return cell;
}
-(void)buttonPressed:(UIButton *)button {
cell.mainLabel = (UILabel *)[contentTable viewWithTag:2];
cell.mainLabel.text = #"goodbye";
}
don't use viewWithTag, use the below to get a reference to the cell
-(void)buttonPressed:(UIButton *)button
{
EntryCell *cell = (EntryCell *)[tblView cellForRowAtIndexPath: [NSIndexPath indexPathForRow:2 inSection:0]];
if(cell != nil)
{
cell.mainLabel.text = #"goodbye";
}
}
EDIT
As david pointed out this will be overwritten if the cell is not visible as cellForRowAtIndexPath: will be called again when it becomes visible. Overwriting this change with whatever you have specified.
You will need to update your data model after this so the change is permanent

Resources