how can i set the table view cell property in an iphone app - objective-c

I have a table view controller like this, and when a row is clicked its colour will turn to blue, but I don't want it to, because I have a text field and a label in each row. How can I stop it from becoming blue like this?
This is my code for cell construction:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCellStudentData *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
}
switch(indexPath.section)
{
case 0:
switch (indexPath.row)
{
case 0:
{
tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(125,15, 170, 40)];
cell.accessoryView = tfText[0];
tfText[0].delegate=self;
tfText[0].text =#"";
tfText[0].placeholder =#"<Student Name>";
cell.primaryLabel.text =#"Student Name: ";
}
break;
case 1:
{
tfText[1] = [[UITextField alloc] initWithFrame:CGRectMake(125,15, 170, 40)];
cell.accessoryView = tfText[1];
tfText[1].delegate=self;
tfText[1].placeholder =#"<Student Age>";
cell.primaryLabel.text =#"Student Age: ";
}
break;
case 2:
{
cell.primaryLabel.text =#"Gender: ";
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
segmentedControl.frame = CGRectMake(100,10,200,30);
[segmentedControl insertSegmentWithTitle:#"Male" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:#"Female" atIndex:1 animated:YES];
segmentedControl.selectedSegmentIndex = 1;
segmentedControl.tintColor = [UIColor purpleColor];
segmentedControl.backgroundColor = [UIColor whiteColor];
[segmentedControl setMomentary:YES];
[segmentedControl addTarget:self action:#selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
cell.accessoryView =segmentedControl;
//[self.view addSubview:segmentedControl];
/*
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
[segmentedControl insertSegmentWithTitle:#"Male" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:#"Female" atIndex:1 animated:YES];
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.frame = CGRectMake(100,10,200,30);
[segmentedControl setMomentary:YES];
[segmentedControl addTarget:self action:#selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
*/
}
break;
case 3:
{
tfText[3] = [[UITextField alloc] initWithFrame:CGRectMake(125,15, 170, 40)];
cell.accessoryView = tfText[3];
tfText[3].delegate=self;
tfText[3].placeholder =#"<DD/MM/YYYY>";
cell.primaryLabel.text =#"Date Of Birth: ";
}
break;
case 4:
{
tfText[5] = [[UITextField alloc] initWithFrame:CGRectMake(125,15, 170, 40)];
cell.accessoryView = tfText[5];
tfText[5].delegate=self;
tfText[5].placeholder =#"<Blood Group>";
cell.primaryLabel.text =#"Blood Group: ";
}
break;
case 5:
{
tfText[6] = [[UITextField alloc] initWithFrame:CGRectMake(125,15, 170, 40)];
cell.accessoryView = tfText[6];
tfText[6].delegate=self;
tfText[6].placeholder =#"<Address>";
cell.primaryLabel.text =#"Address: ";
}
break;
default:
break;
}

Actually it is only one line of code...
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

Inside your method i.e cellforrowatindexpath add the below code
UIView *bckv = [[UIView alloc] initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width,cell.frame.size.height)];
bckv.backgroundColor = [UIColor colorWithRed:106/256.0 green:108.0/256.0 blue:107.0/256.0 alpha:0.0];
cell.selectedBackgroundView=bckv;
[bckv release];
OR
cell.selectionStyle=UITableViewCellSelectionStyleNone;
Hope it helps......

cell.selectionStyle = UITableViewCellSelectionStyleNone;

Related

UIImageView transparent black on a cell bug (Objective-C)

I have a UIImageView installed on my cell, it is transparent black, but when I scroll the CollectionView and I raise my UIImageView but there is always more to transprence.
See picture :
1 - I is not even scroll
2 - After a scroll
My code :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"pictureCell";
MSContestListCollectionViewCell *cell = (MSContestListCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.titleContest.adjustsFontSizeToFitWidth = YES;
cell.titleContest.minimumScaleFactor = 0.5;
cell.pictureImageView.layer.cornerRadius = 5;
cell.pictureImageView.clipsToBounds = YES;
cell.titleView.layer.cornerRadius = 5;
cell.titleView.clipsToBounds = YES;
switch (_segmentedControl.selectedSegmentIndex) {
case 0: {
NSDictionary *searchResult = [self.readArray objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.statusContest.text = [searchResult objectForKey:#"status"];
if ([[searchResult objectForKey:#"status"] isEqualToString:#"PAUSE"]) {
cell.titleContest.text = [NSString stringWithFormat:#"Concours en pause"];
/*
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];
view.layer.cornerRadius = 5;
view.clipsToBounds = YES;
[cell.contentView addSubview:view];
*/
UIImageView *imagecellPause = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
UIImage *cellImage = [UIImage imageNamed:#"cell-pause.png"];
imagecellPause.image = cellImage;
[cell.contentView addSubview:imagecellPause];
UIImageView *imagePause = [[UIImageView alloc] initWithFrame:CGRectMake(69.5, 69.5, 25, 25)];
UIImage *image = [UIImage imageNamed:#"Pause Filled-50 (1).png"];
imagePause.image = image;
[cell.contentView addSubview:imagePause];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 150, 50)];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"Avenir-Book" size:12];
label.text = [searchResult objectForKey:#"description"];
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5;
[cell.contentView addSubview:label];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 105, 150, 50)];
label2.textColor = [UIColor whiteColor];
label2.font = [UIFont fontWithName:#"Avenir-Black" size:15];
label2.text = #"Concours en pause";
[cell.contentView addSubview:label2];
cell.titleView.hidden = YES;
} else {
cell.titleContest.text = [searchResult objectForKey:#"description"];
}
break;
}
case 1: {
NSDictionary *searchResult2 = [self.readArrayWinner objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult2 objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.titleContest.text = [searchResult2 objectForKey:#"description"];
cell.statusContest.text = [searchResult2 objectForKey:#"status"];
NSLog(#"le gagnant : %#", [searchResult2 valueForKeyPath:#"winners.name"]);
break;
}
case 2: {
NSDictionary *searchResult3 = [self.readArrayPhotos objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult3 objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.titleContest.text = [searchResult3 objectForKey:#"description"];
cell.statusContest.text = [searchResult3 objectForKey:#"status"];
break;
}
default:
break;
}
[_activity stopAnimating];
_label1.hidden = YES;
return cell;
}
Your CellForRowAtIndexPath should never contain addSubView, since everytime the cell is reloaded it will add the view. Thats why it keeps getting darker, it just adds view onto view.
Instead you should subclass your UICollectionViewCell and add the view in the subclass.

UITextView on cell clears when scrolling

Hello fellow programmers,
I am really cracking my head trying to understand what the problem is, but I just can't seem to figure out the problem. I have done research on this but nothing works for me unfortunately. The problem is that I have multiple cells and each cell is given a specific UITextField which I draw on the cellForRowAtIndexPath programmatically. The issues is that when I enter some information on that any Text Field and I scroll down on the table view, the text on the text field disappears into thin air. I am trying to understand why the cell reuse (which I believe is causing the problem), is causing this.
Here is my code for the cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
}
cellText = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 260, 20)];
cellText.font = [UIFont boldSystemFontOfSize:11.0];
cellText.textColor = [UIColor grayColor];
[cell.contentView addSubview:cellText];
if (indexPath.section == 0)
{
cellText.text = clientInfoArray[indexPath.row];
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Customer Name"])
{
nameTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
nameTxtField.placeholder = #"full name";
nameTxtField.font = [UIFont boldSystemFontOfSize:15.0];
nameTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[nameTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:nameTxtField];
}
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Phone"])
{
phoneTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
phoneTxtField.placeholder = #"xxx-xxx-xxx";
phoneTxtField.font = [UIFont boldSystemFontOfSize:15.0];
phoneTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
phoneTxtField.keyboardType = UIKeyboardTypePhonePad;
[phoneTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
[cell addSubview:phoneTxtField];
}
if ([[clientInfoArray objectAtIndex:indexPath.row] isEqual:#"Date"])
{
dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
dateLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:dateLabel];
dateLabel.text = [dateFormatter stringFromDate:[NSDate date]];
}
}
if (indexPath.section == 1) {
cellText.text = vehicleInfoArray[indexPath.row];
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Make and Model"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
modelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
modelLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:modelLabel];
modelLabel.text = makeAndModelData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Color"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
colorLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
colorLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:colorLabel];
colorLabel.text = colorData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Doors"])
{
doorsTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
doorsTxtField.placeholder = #"#";
doorsTxtField.font = [UIFont boldSystemFontOfSize:15.0];
doorsTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[doorsTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
doorsTxtField.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:doorsTxtField];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Vehicle VIN"])
{
vinTxtField = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
vinTxtField.placeholder = #"#";
vinTxtField.font = [UIFont boldSystemFontOfSize:15.0];
vinTxtField.autocorrectionType = UITextAutocorrectionTypeNo;
[vinTxtField setClearButtonMode:UITextFieldViewModeWhileEditing];
vinTxtField.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:vinTxtField];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Fuel"])
{
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
fuelLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
fuelLabel.font = [UIFont boldSystemFontOfSize:15.0];
[cell.contentView addSubview:fuelLabel];
fuelLabel.text = fuelData;
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Milage"])
{
milageLabel = [[UITextField alloc] initWithFrame:CGRectMake(15, 35, 300, 30)];
milageLabel.placeholder = #"ODO";
milageLabel.font = [UIFont boldSystemFontOfSize:15.0];
milageLabel.autocorrectionType = UITextAutocorrectionTypeNo;
[milageLabel setClearButtonMode:UITextFieldViewModeWhileEditing];
milageLabel.keyboardType = UIKeyboardTypeNumberPad;
[cell addSubview:milageLabel];
}
if ([[vehicleInfoArray objectAtIndex:indexPath.row] isEqual:#"Other description"])
{
otherDTxtView = [[UITextView alloc] initWithFrame:CGRectMake(15, 35, 300, 125)];
otherDTxtView.font = [UIFont boldSystemFontOfSize:15.0];
[cell addSubview:otherDTxtView];
}
}
if (indexPath.section == 2) {
cellText.text = InAndOutInfoArray[indexPath.row];
}
return cell;
}
Any ideas on how I can fix this problem? + Extra points for a helpful answer. :)
Whenever you are using reuse cell techniques, the cell that is not visible will be deallocated to free some memory.
Since you are creating a textfield in the cellForRow method, it will also be deallocated when the cell is not visible and redrawn when it becomes visible.
You could save the textfield value in some array (using textfield's didEndEditing delegate) and use that array to populate the text during textfield creation process inside the cellForRow method.

Disable UITextField in UITableViewCell

I have a UITextField in a UITableViewCell.
Even though I set -
textField.userInteractionEnabled = NO;
textField.enabled = NO
But when I click on the table cell which contains the textField, the keyboard comes up for the textfield.
Why is this happening and how can I prevent it?
EDIT: Strangely this is happening when I first set some text in the textfield. When the textfield is empty, it is not editable.
EDIT: Code for cellForRowAtIndexPath -
cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
cell.accessoryType = UITableViewCellAccessoryNone;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, cell.bounds.size.width - 20, cell.bounds.size.height - 20)];
textField.font = [UIFont systemFontOfSize:15];
textField.textColor = [UIColor blackColor];
UIColor *placeholderColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self getPlaceHolderTextForIndexPath:indexPath] attributes:#{NSForegroundColorAttributeName : placeholderColor}];
textField.keyboardType = [self getKeyboardTyeForIndexPath:indexPath];
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.textAlignment = NSTextAlignmentLeft;
textField.autocapitalizationType = [self getAutocapitaliztionTypeForIndexPath:indexPath];
textField.tag = 1;
if (_editingNotAllowed) {
[textField setText:[self getTextForTextFieldWithIndexPath:indexPath]];
[textField setUserInteractionEnabled:NO];
textField.enabled = NO;
} else {
[textField setUserInteractionEnabled:YES];
}
[cell.contentView addSubview:textField];
You should create an UITableViewCell as shown in this repo :)
https://github.com/breeno/EditingUITableView
And use your custom UITableViewCell like this:
if(condition){ // Check the row must be TextField
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(!cell){
cell = [[CustomCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: nil];
}
cell.label.text = #"Title Row"; //UITextLabel Title
UIColor *placeholderColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1];
cell.textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self getPlaceHolderTextForIndexPath:indexPath] attributes:#{NSForegroundColorAttributeName : placeholderColor}];
cell.textField.returnKeyType = UIReturnKeyDone;
cell.textField.backgroundColor = [UIColor clearColor];
cell.textField.textAlignment = NSTextAlignmentLeft;
cell.textField.tag = 1;
} else { // Normal UITableViewCell
}

reloading data of a table view is not working

im making a tableview with checkboxes. I did implement the checkboxes with UIButtons and i can check and uncheck them without problems. The problem came up when i tried to make a "select/unselect all" button and this is the resultant code:
-(IBAction)select:(id)sender{
if (all==YES) {
all=NO;
}
else {
all=YES;
}
[tblPeticiones reloadData];
}
The problem is that the table doesn't reload the data.
Any idea?
Thanks and regards.
EDIT:
I load de data like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//Datos
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *fInicio = [dateFormatter stringFromDate:[(MAP_Gastos_CiberTRIPS *)[m objectAtIndex:indexPath.row] DEP_DATE]];
NSString *loc = [(MAP_Gastos_CiberTRIPS *)[m objectAtIndex:indexPath.row] LOCATION];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[formatter setCurrencyCode:#"EUR"];
[formatter setLocale:[NSLocale currentLocale]];
NSString *precio = [formatter stringFromNumber:aux];
//Vista
NSString *MyIdentifier = [NSString stringWithFormat:#"MyIdentifier %i", indexPath.row];
CustomTVC *cell = (CustomTVC *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[CustomTVC alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
//CheckBox
UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[checkButton setFrame:CGRectMake(10, 10, 23, 23)];
if (todos==NO) {
[checkButton setBackgroundImage:[[UIImage imageNamed:#"checkNO.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
checkButton.tag = 0;
}
else {
[checkButton setBackgroundImage:[[UIImage imageNamed:#"checkSI.png"] str etchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
checkButton.tag = 1;
}
[checkButton addTarget:self action:#selector(checkAction:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:checkButton];
[cell.contentView addSubview:checkButton];
//fecha
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(40, 0, 70.0,tableView.rowHeight)] autorelease];
[cell addColumn:0];
label.tag = 1;
label.font = [UIFont boldSystemFontOfSize:12.0];
label.text = fInicio;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor blackColor];
//label.backgroundColor = [UIColor whiteColor];
label.autoresizingMask = UIViewAutoresizingNone | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
if (indexPath.row % 2 == 0){
label.backgroundColor = [UIColor colorWithRed:233.0/255.0
green:233.0/255.0
blue:233.0/255.0
alpha:1.0];
} else {
label.backgroundColor = [UIColor clearColor];
}
//localizaciĆ³n
label = [[[UILabel alloc] initWithFrame:CGRectMake(115, 0, 75.0,tableView.rowHeight)] autorelease];
[cell addColumn:180];
label.tag = 2;
label.font = [UIFont boldSystemFontOfSize:12.0];
label.text = loc;
label.textAlignment = UITextAlignmentLeft;
label.textColor = [UIColor blackColor];
//label.backgroundColor = [UIColor whiteColor];
label.autoresizingMask = UIViewAutoresizingNone | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
if (indexPath.row % 2 == 0){
label.backgroundColor = [UIColor colorWithRed:233.0/255.0
green:233.0/255.0
blue:233.0/255.0
alpha:1.0];
} else {
label.backgroundColor = [UIColor clearColor];
}
}
return cell;
}
Debug the problem
Check datasource methods of tableView whether they are being called or not after reload?
You should store which rows are checked in an array. Then check this array when you load / reload the tableview (in cellForRowAtIndex) to see if the row should be checked.
Then if you want to select all or none, just delete or add them to the array and reload the tableview.
If you need help implementing this, let me know.
i found the problem. This is the working code:
-(IBAction)select:(id)sender{
if (all==YES) {
all=NO;
[btnSelAll setBackgroundImage:[[UIImage imageNamed:#"checkSI.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
}else {
all=YES;
[btnSelAll setBackgroundImage:[[UIImage imageNamed:#"checkNO.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
}
NSMutableArray *arrIndex = [NSMutableArray new];
for(int i=0;i<[m count];i++) {
[arrIndex addObject:[NSIndexPath indexPathForRow:i inSection:0]];
}
[self.tblPeticiones beginUpdates];
[tblPeticiones reloadRowsAtIndexPaths:arrIndex withRowAnimation:UITableViewRowAnimationNone];
[self.tblPeticiones endUpdates];
[tblPeticiones reloadData];
}
Some comments about your code:
There seems to be global variable called all, whose value you toggle, when button is pressed. However inside cellForRowAtIndexPath you check value of another global variable called todos. Is there some connection between these variables?
You change checkbox background image only, when you create a new cell. Since cells are recycled, you need to move this code outside if (cell == nil) i.e. afterwards, where you are customizing a recycled cell
You should only check whether if (todos) and if (!todos), don't compare to values like YES or NO. Also easiest way to toggle a boolean is todos = !todos :)

UITableView section not reloading when called

I want a section of my table view to reload whenever the ViewWillAppear method is called, I've implemented this like so:
- (void)viewWillAppear:(BOOL)animated {
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:0 inSection:1];
reloadRows = [NSArray arrayWithObjects:rowToReload, nil];
[self.tableView reloadRowsAtIndexPaths:reloadRows withRowAnimation:UITableViewRowAnimationNone];
}
Here is the rowforsection method that indicates which content should appear in each tableview section:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"fadk");
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"PINGAS"];
[self.tableView setAlwaysBounceVertical:YES];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:#"PINGAS"] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
// if ([indexPath section] == 0) {
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 3, 300, 41)];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 0, 300, 120)];
UIView *paddingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 0)] autorelease];
paddingView.backgroundColor = [UIColor cyanColor];
// if ([indexPath row] == 0) {
if ([indexPath section] == 0) {
NSLog(#"0");
[cell addSubview:textField];
if ([indexPath row] == 0) {
textField.placeholder = #"Title";
}
else{
textField.placeholder = #"Location";
}
}
else if ([indexPath section] == 1) {
NSLog(#"1");
NSDateFormatter *formatter;
NSString *eSString1;
NSString *eEString2;
formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"h:mm a"];
cell.textLabel.text = #"Starts\nEnds";
cell.textLabel.numberOfLines = 2;
eSString1 = [formatter stringFromDate:eSTime];
eEString2 = [formatter stringFromDate:eEtime];
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#\n%#", eSString1, eEString2];
cell.detailTextLabel.numberOfLines = 2;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
else{
NSLog(#"2");
[cell addSubview:textView];
}
textField.delegate = self;
textField.leftView = paddingView;
textField.leftViewMode = UITextFieldViewModeAlways;
textField.adjustsFontSizeToFitWidth = YES;
textField.textColor = [UIColor blackColor];
textField.keyboardType = UIKeyboardTypeAlphabet;
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
textField.textAlignment = UITextAlignmentLeft;
textField.tag = 0;
//playerTextField.delegate = self;
textField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
[textField setEnabled: YES];
[textField release];
textView.delegate = self;
textView.textColor = [UIColor blackColor];
textView.keyboardType = UIKeyboardTypeAlphabet;
textView.returnKeyType = UIReturnKeyDone;
textView.backgroundColor = [UIColor clearColor];
textView.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
textView.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
textView.textAlignment = UITextAlignmentLeft;
textView.tag = 0;
[textView release];
// }
}
return cell;
}
This works swimmingly the first load, and I after the first calling of viewWillAppear, but after that the section seems to recycle the data from the first load and the second load, and while it still enters the cellforrow section, it no longer goes into the section I call in the viewWIllAppear section.
The reload should be sandwiched between begin / end updates:
[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:reloadRows withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];