xCode: TableView cant see/acces all cells - objective-c

When I press a button called "AllOK" I want the object.selectedIndex to be 0. This works perfectly with the cells that is visible. But it won't acces the cells which isn't viewable on the app / screen. If you scroll down and get vision of them, it will check them, but i want it to do it, without having to scroll down.
Do anyone know how to get the tableview to know that it "also" got the cells that it cannot see?
My code for my tableview and for the button:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"FancyCell"];
cell = nil;
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"FancyCell"];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
// add the segmentedControl when you create a new cell
UIImage *correctImageGreen = [[UIImage imageNamed:#"green.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *correctImageGul = [[UIImage imageNamed:#"gul.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *correctImageRed = [[UIImage imageNamed:#"red.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *correctImageGray = [[UIImage imageNamed:#"gray.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
NSArray *itemArray = [NSArray arrayWithObjects: correctImageGreen, correctImageGul, correctImageRed, correctImageGray, nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(310, 7, 150, 30);
[cell.contentView addSubview:segmentedControl];
// add an action so we can change our model if the view changes
[segmentedControl addTarget:self action:#selector(didChangeSegmentedControl:) forControlEvents:UIControlEventValueChanged];
// use a tag so we can retrieve the segmentedControl later
segmentedControl.tag = 42;
}
// either if the cell could be dequeued or you created a new cell,
// segmentedControl will contain a valid instance
UISegmentedControl *segmentedControl = (UISegmentedControl *)[cell.contentView viewWithTag:42];
UIImage *comment = [UIImage imageNamed:#"Checkmark-hvid"];
UIImage *imageRef = [UIImage imageNamed:#"Checkmark-hvid"];
UIImageView *commentView = [[UIImageView alloc] initWithImage: comment];
UIImageView *imageRefView = [[UIImageView alloc] initWithImage: imageRef];
commentView.frame = CGRectMake(625, 5, 30, 30);
imageRefView.frame = CGRectMake(515, 5, 30, 30);
commentView.tag = 98;
imageRefView.tag = 99;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
boolean_t didGetStates = [defaults boolForKey:#"didGetStates"];
MBFancyObject *object = _objects[indexPath.row];
if (didGetStates) {
// State
NSDictionary *dic = [tableData objectAtIndex:indexPath.row];
if (object.beingEdited == -1) {
int selectedState = [[dic valueForKey:#"State"] intValue];
object.selectedIndex = selectedState;
}
// Comment & ImageRef
int comment = [[dic valueForKey:#"Comment"] intValue];
int imageRef = [[dic valueForKey:#"Foto"] intValue];
if (comment == 0) {
[cell.contentView addSubview:commentView];
}
else {
[[cell.contentView viewWithTag:98]removeFromSuperview];
}
if (imageRef == 0) {
[cell.contentView addSubview:imageRefView];
}
else {
[[cell.contentView viewWithTag:99]removeFromSuperview];
}
}
cell.textLabel.text = object.title;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
segmentedControl.selectedSegmentIndex = object.selectedIndex;
object.currentIndexRow = indexPath.row;
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
return cell;
}
- (IBAction)allOK:(id)sender {
for (MBFancyObject *object in _objects) {
object.selectedIndex = 0;
object.beingEdited = 0;
}
[[self tableView] reloadData];
}

So I hadn't noticed this at my first comment, but your problem here is obvious. You are not properly dequeueing cells from the cell pool, and therefore when you scroll, you are creating a brand new cell EVERY time, which causes them not to have your selected index the way you want. I am pretty sure that your solution lies right here:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"FancyCell"];
cell = nil;
if (!cell) {
You are dequeuing a cell properly, but then immediately setting it to nil, meaning it will ALWAYS go through the !cell check and will ALWAYS create a new cell. Try removing this line and working with the dequeued cell.
Edit:
Since that didn't do it, this is what else I would try:
Create a call to the method willDisplayCell:forRowAtIndexPath:, and in this call check a boolean flag that you set to see if the segmented control at that index should be at index 0 or not. So basically whenever a cell is about to be shown, you check if it's segmented control should be set to index 0, and if it should, set it's index to 0.

Related

Multiple Segment is Selected in SegmentController in iOS7

I have add one SegementController in CellForRowAtIndex method and its selected lasted value which we select previously. But once we move to other screen and come back to current then Segement will select the multiple option like this :
Here "Yes" is already selected and then i select "No",
Here is the code which i write in CellForRowAtIndexPath :
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Yes", #"No", #"Either", nil]];
seg.frame = CGRectMake(190.0f, 10.0f, 205.0f, 30.0f);
seg.selectedSegmentIndex = 0;
[seg setTintColor:[UIColor orangeColor]];
seg.segmentedControlStyle = UISegmentedControlStyleBar;
[seg addTarget:self action:#selector(segSelected:) forControlEvents:UIControlEventValueChanged];
seg.tag = indexPath.row;
cell.textLabel.frame = CGRectMake(10, 5, 150, 30);
[cell.contentView addSubview:seg];
seg.selectedSegmentIndex = [[[arryaSegment objectAtIndex:indexPath.row]objectForKey:#"SelecteKey"] intValue];
This is probably not about selecting 2 segments at the same time, but adding two segmentedControls at the same time because you add a new control each time the cell is displayed.
Make sure the code that adds the UISegmentedControl is only called once per cell.
e.g:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell; // = ... dequeue ...
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
// add segmented control only when creating a new cell
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Yes", #"No", #"Either", nil]];
seg.frame = CGRectMake(190.0f, 10.0f, 205.0f, 30.0f);
seg.selectedSegmentIndex = 0;
[seg setTintColor:[UIColor orangeColor]];
seg.segmentedControlStyle = UISegmentedControlStyleBar;
[seg addTarget:self action:#selector(segSelected:) forControlEvents:UIControlEventValueChanged];
cell.textLabel.frame = CGRectMake(10, 5, 150, 30);
[cell.contentView addSubview:seg];
seg.tag = 42; // used to reference segmentedControl in dequeued cell. see next line
}
UISegmentedControl *seg = (UISegmentedControl *)[cell.contentView viewWithTag:42];
seg.selectedSegmentIndex = [[[arryaSegment objectAtIndex:indexPath.row]objectForKey:#"SelecteKey"] intValue];
return cell;
}
since you can't use the ugly way of getting the indexPath with the tag of the segmentedControl anymore, you have to use a method which gets the indexPath in a different way. Something like this:
- (IBAction)segSelected:(UISegmentedControl *)sender {
CGPoint originOfSegmentedControlInTableView = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:originOfSegmentedControlInTableView];
...
}

UIImage with UITapGestureRecognizer in first row of UITableViewController not working properly

I've got a UITableViewController subclass that is subclassed by two other classes. In some of the cells of both tables I have an image that the user can click on to perform an action. It all works and I'm happy except that in one of the 2 subclasses the first row of that table has an issue. You can only click the image by clicking to the rightmost edge of the image. If you click in the body of the image it calls the table's didSelectRowAtIndexPath instead.
What boggles me is that it works in the other class for all rows and even in the one it doesn't work for it works in all the other rows where that image shows up. Here's the code I'm using to add the image and gesture recognizer into my cell:
UIImageView *imgparent = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain, 24.0, 14.0, 16.0)];
imgparent.image = [UIImage imageNamed:#"item_open.png"];
imgparent.tag = ITEMOPENTAG;
// add listener
imgparent.userInteractionEnabled = YES;
UITapGestureRecognizer *singleFingerDTapParent = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(loadChildren:)];
[imgparent addGestureRecognizer:singleFingerDTapParent];
[singleFingerDTapParent release];
[cell.contentView addSubview:imgparent];
[imgparent release];
I'm testing in the simulator by the way. I've tried replacing the image with a button but still the problem persists. Any ideas?
EDIT: Here's the beginnings of the code that is called when the image is clicked. Again, this works but in the case of that one row it only works if you click on the right edge of the image. I've attached a screenshot to illustrate. The first circle indicates where I have to tap in order to get my gesture to be called. The second shows an example in the very same table that allows you to click the entire image. I'm confounded.
- (void)loadChildren:(UIGestureRecognizer *)gestureRecognizer {
NSLog(#"loadChildren");
// get the cell
UITableViewCell *cell = (UITableViewCell *)gestureRecognizer.view.superview.superview;
UIImageView *imgvw = (UIImageView *)[cell viewWithTag:ITEMOPENTAG]; // open arrow
[imgvw setHidden:YES]; // hide open arrow
imgvw = (UIImageView *)[cell viewWithTag:ITEMCLOSETAG]; // close arrow
[imgvw setHidden:NO]; // show close arrow
// get the record for the cell
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
}
EDIT: Here's the complete tableView:cellForRowAtIndexPath:indexPath. Keep in mind that it's being called from the cellForRowAtIndexPath of 2 other subclasses of this class, each simply passing in an identifier withType:
- (UITableViewCell *)tableView:(UITableView *)tv
cellForRowAtIndexPath:(NSIndexPath *)indexPath
withType:(NSString *)s_type
{
UITableViewCell *cell = nil;
// if dealing with empty region then show no tasks cell
if(s_type == #"noTasksCell"){
cell = [tv dequeueReusableCellWithIdentifier:#"noTasksCell"];
if( cell == nil ) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"noTasksCell"] autorelease];
}
cell.textLabel.text = #"No tasks";
cell.textLabel.textColor = [UIColor lightGrayColor];
cell.textLabel.font = [UIFont systemFontOfSize:14];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
UILabel *lblMain;
NSDictionary *o_rec = [self getRecForPath:indexPath];
NSString *s_cell = #"rowCell";
BOOL b_parent = NO;
BOOL b_parentOpen = NO;
BOOL b_child = NO;
BOOL b_checked = NO;
if([self isParent:indexPath]){
b_parent = YES;
b_parentOpen = !([o_rec objectForKey:#"b_open"] == nil || [[o_rec objectForKey:#"b_open"] isEqualToNumber:[NSNumber numberWithInt:0]]);
s_cell = [s_cell stringByAppendingString:#"Parent"];
}
if([o_rec objectForKey:#"b_child"] != nil){
b_child = YES;
s_cell = [s_cell stringByAppendingString:[NSString stringWithFormat:#"Child%#",[o_rec objectForKey:#"indent"]]];
}
if([[o_rec objectForKey:#"checked"] isEqualToNumber:[NSNumber numberWithInt:1]]){
b_checked = YES;
s_cell = [s_cell stringByAppendingString:#"isComplete"];
}
// add the following to the name:
// - project id
// - width of table to the name so that rotations will change the cell dequeue names
// - priority
s_cell = [s_cell stringByAppendingFormat:
#"Proj%#Width%dP%#"
,[o_rec objectForKey:#"project_id"]
,(int)tv.bounds.size.width
,[[o_rec objectForKey:#"priority"] stringValue]
];
cell = [tv dequeueReusableCellWithIdentifier:s_cell];
if( cell == nil ) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:s_cell] autorelease];
cell.textLabel.hidden = YES; // hide the regular text label
cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
cell.selectedBackgroundView.backgroundColor = [delegate colorForHexWithAlpha:0xffcc66ff];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
int offsetleftmain = 10;
if(b_child){
offsetleftmain += ([[o_rec objectForKey:#"indent"] intValue]-1) * 18;
}
if(b_parent){
// parent arrow
UIImageView *imgparent = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain, 24.0, 14.0, 16.0)];
imgparent.image = [UIImage imageNamed:#"item_open.png"];
imgparent.tag = ITEMOPENTAG;
// add listener
imgparent.userInteractionEnabled = YES;
UITapGestureRecognizer *singleFingerDTapParent = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(loadChildren:)];
singleFingerDTapParent.numberOfTapsRequired = 1;
singleFingerDTapParent.numberOfTouchesRequired = 1;
[imgparent addGestureRecognizer:singleFingerDTapParent];
[singleFingerDTapParent release];
[cell.contentView addSubview:imgparent];
[imgparent release];
// close arrow
UIImageView *imgparent2 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain-2, 24.0, 16.0, 14.0)];
imgparent2.image = [UIImage imageNamed:#"item_close.png"];
imgparent2.tag = ITEMCLOSETAG;
imgparent2.userInteractionEnabled = YES;
UITapGestureRecognizer *singleFingerDTapParent2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(hideChildren:)];
[imgparent2 addGestureRecognizer:singleFingerDTapParent2];
[singleFingerDTapParent2 release];
[cell.contentView addSubview:imgparent2];
[imgparent2 release];
}
offsetleftmain += 18;
// checkbox
UIImageView *imgchk = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain, 20.0, 24.0, 24.0)];
imgchk.image = [UIImage imageNamed:#"check_empty.png"];
imgchk.tag = EMPTYCHECKTAG;
// add listener
imgchk.userInteractionEnabled = YES;
UITapGestureRecognizer *singleFingerDTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(checkOnItem:)];
[imgchk addGestureRecognizer:singleFingerDTap];
[singleFingerDTap release];
[cell.contentView addSubview:imgchk];
[imgchk release];
// checked checkbox
UIImageView *imgchk2 = [[UIImageView alloc] initWithFrame:CGRectMake(offsetleftmain, 20.0, 24.0, 24.0)];
imgchk2.image = [UIImage imageNamed:#"check_checked.png"];
imgchk2.tag = CHECKTAG;
// add listener
imgchk2.userInteractionEnabled = YES;
UITapGestureRecognizer *singleFingerDTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(checkOffItem:)];
[imgchk2 addGestureRecognizer:singleFingerDTap2];
[singleFingerDTap2 release];
[cell.contentView addSubview:imgchk2];
[imgchk2 release];
offsetleftmain += 28;
// main label
lblMain = [[UILabel alloc] initWithFrame:CGRectMake(offsetleftmain, 22.0, tv.bounds.size.width-offsetleftmain-10, 30.0)];
lblMain.tag = MAINLABELTAG;
lblMain.numberOfLines = 4;
lblMain.font = delegate.font_dflt;
// change color based on priority
if (5-[[o_rec objectForKey:#"priority"] intValue] == 1)
lblMain.textColor = [UIColor redColor];
else if (5-[[o_rec objectForKey:#"priority"] intValue] == 2)
lblMain.textColor = [delegate colorForHexWithAlpha:H_P2COLOR];
else if (5-[[o_rec objectForKey:#"priority"] intValue] == 3)
lblMain.textColor = [delegate colorForHexWithAlpha:H_P3COLOR];
[cell.contentView addSubview:lblMain];
[lblMain release];
// show action sheet for long press and hold
UILongPressGestureRecognizer *clicknHold = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(cellClicknHold:)];
[cell addGestureRecognizer:clicknHold];
[clicknHold release];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// task text
lblMain = (UILabel *)[cell viewWithTag:MAINLABELTAG];
lblMain.text = [self formatContent:[o_rec objectForKey:#"content"]];
CGRect newFrame = lblMain.frame;
newFrame.size.height = [[o_rec objectForKey:#"height"] floatValue];
//newFrame.size.height = [[o_rec objectForKey:#"height"] floatValue]+12;
lblMain.frame = newFrame;
// set checked status
[(UIImageView *)[cell viewWithTag:EMPTYCHECKTAG]
setHidden:[[o_rec objectForKey:#"checked"] boolValue]];
[(UIImageView *)[cell viewWithTag:CHECKTAG]
setHidden:![[o_rec objectForKey:#"checked"] boolValue]];
// show open arrow if dealing with parent cell
if (b_parent) {
//NSLog(#"b_parentOpen:%d",b_parentOpen);
[(UIImageView *)[cell viewWithTag:ITEMOPENTAG] setHidden:b_parentOpen];
[(UIImageView *)[cell viewWithTag:ITEMCLOSETAG] setHidden:!b_parentOpen];
}
return cell;
}
Just in case someone runs into a similar issue as rare as that may be maybe this will help avoid someone going bald pulling their hair out. So my problem wasn't with the tableView itself but a sidebar I have that is similar to the facebook sidebar that slides in and out. My sidebar has a tableview in it and that table has a header on it that for some reason when it was right beside my other table was somehow preventing clicks to the image in that first row. My solution was simply to position that sidebar view a little bit further offscreen away from the view it was beside instead of being right next to it. Problem solved. Thanks to all who tried to help me out.

Conditional adding an image to custom cell.contentView property

I want to create a UITableViewCell with an image between the content and the accessory view, sonly if certain condition is met.
So I have to create a custom content view with two UILabel and UIImageView as described in "Table View Programming Guide for iOS".
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UILabel *mainLabel;
UILabel *secondLabel;
UIImageView *icon;
YOEvento *aux = [[self.eventosListsContainer objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// 1. Check if it is a favourtite to display the icon
if (aux.isFavourite) {
// Evento is favourite
// 1. Create the main label view
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(20, 8, 248, 14)] autorelease];
mainLabel.tag = MAINLABEL_TAG;
mainLabel.font = [UIFont systemFontOfSize:13.0];
mainLabel.textColor = [UIColor darkGrayColor];
mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:mainLabel];
// Create the date label
secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(23, 24, 245, 11)] autorelease];
secondLabel.tag = SECONDLABEL_TAG;
secondLabel.font = [UIFont systemFontOfSize:11.0];
secondLabel.textColor = [UIColor lightGrayColor];
secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:secondLabel];
// Create the image
icon = [[[UIImageView alloc] initWithFrame:CGRectMake(268, 12, 24, 21)] autorelease];
icon.tag = ICON_TAG;
icon.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:icon];
} else {
// Evento is not favourite
// 1. Create the main label view
mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(20, 8, 282, 14)] autorelease];
mainLabel.tag = MAINLABEL_TAG;
mainLabel.font = [UIFont systemFontOfSize:13.0];
mainLabel.textColor = [UIColor darkGrayColor];
mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:mainLabel];
// Create the date label
secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(23, 24, 279, 11)] autorelease];
secondLabel.tag = SECONDLABEL_TAG;
secondLabel.font = [UIFont systemFontOfSize:11.0];
secondLabel.textColor = [UIColor lightGrayColor];
secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:secondLabel];
}
} else {
//
if (aux.isFavourite) {
mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
icon = (UIImageView *)[cell.contentView viewWithTag:ICON_TAG];
} else {
mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
}
}
// Load cell values
if (aux.isFavourite) {
mainLabel.text = aux.nombre;
secondLabel.text = #"16 de Octubre"; // ?????????????
icon.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"29-heart"
ofType:#"png"]];
} else {
mainLabel.text = aux.nombre;
secondLabel.text = #"16 de Octubre"; // ?????????????
}
return cell;
}
The problem is that the icon image is loaded randomly because it keeps showing when reusing cells.
I have to admit that I don't fully understand the reusing cells theory.
In my original implementation (using the imageView property) it was easy, I just check is the event was not a favorite to set cell.imageView = nil. But have no idea how to do with custom contentView.
Once you create your cell, you are wanting to re-arrange its contents - this is adding unnecessary complexity. Instead you should create two different cell types - one for a favourite and one for a non-favourite. Try something like this:
static NSString *CellIdentifierFavourite = #"CellFavourite";
static NSString *CellIdentifierNonFavourite = #"CellNonFavourite";
// Other setup goes here
YOEvento *aux = [[self.eventosListsContainer objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
if (aux.isFavourite) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierFavourite];
if (cell == nil) {
// Create favourite cell here
}
// Populate favourite cell here
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierNonFavourite];
if (cell == nil) {
// Create non-favourite cell here
}
// Populate non-favourite cell here
}
// Rest of method goes below

Objective C: How to implement ABTableViewCell?

I am currently facing serious scrolling performance issues with my UITableView. I have the following subviews in my custom cell
1 X UIImageView
2 X UILabel
3 X UIButtons
I read on the net that having multiple subviews in a custom cell will have serious implications to scrolling performance. I found ABTableViewCell but am not sure how to go about porting my current code over to it.
Snippet of my current code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* PlaceholderCellIdentifier = #"PlaceholderCell";
int row = [indexPath row];
SomeClass *thisClass = [self.someClassArray objectAtIndex:row];
CustomCell* cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
if (cell == nil)
{
cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier] autorelease];
//Set Avatar imageview
UIImageView *avatarView = [[UIImageView alloc]initWithFrame:CGRectMake(5, CELL_SPACING,CELL_AVATAR_WIDTH, CELL_AVATAR_HEIGHT)];
avatarView.tag = 1;
//Set text Label
UILabel *textLabel = [[UILabel alloc]initWithFrame:CGRectZero];
textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:13.0];
[textLabel setLineBreakMode:UILineBreakModeWordWrap];
textLabel.numberOfLines = 0;
textLabel.tag = 2;
//Add button1
UICustomButton *button = [[UICustomButton alloc]init];
button.tag = 3;
[self.contentView addSubview:avatarView];
[self.contentView addSubview:textlabel];
[self.contentView addSubview:button];
}
//Set Avatar imageview
UIImageView *thisAvatarView = (UIImageView *)[self.contentView viewWithTag:1];
thisAvatarView.image = self.dataForCell.user.avatar;
//Set data to text Label
NSString *text = [NSString stringWithFormat:#"%#",self.dataForCell.text];
CGFloat answerLabelHeight = [CustomCell getHeightOfLabel:self.dataForCell ofType:#"XXX"];
UILabel *thisTextLabel = (UILabel*)[self.contentView viewWithTag:2];
[thisTextLabel setFrame:CGRectMake(CELL_TEXT_LEFT_MARGIN, CELL_SPACING+CELL_USERNAMELABEL_HEIGHT+questionLabelheight,CELL_CONTENT_WIDTH - CELL_TEXT_LEFT_MARGIN*1.5, answerLabelHeight)];
thisTextLabel.text = someData;
//Set data to button
UICustomButton *thisButton = (UICustomButton *)[self.contentView viewWithTag:3];
[thisButton setButtonWithAnswer:self.dataForCell buttonType:#"like" navcon:self.navcon andFrame:CGRectMake(CELL_TEXT_LEFT_MARGIN, totalCommentLabelHeight + CELL_SPACING*4, 45, CELL_BUTTON_HEIGHT)];
thisLikeButton.imageView.image = [UIImage imageNamed:#"xxxx.png"];
}
Sorry for the long code, but can anyone guide me along on how I can start to use ABTableViewCell for this implementation?

Weird updates when using reloadRowsAtIndexPaths: in a table

I've got a table where the accessoryView button in the cell is replaced by a custom check mark icon. If the time interval for a pair of temperature settings is enabled, the cell row shows a check mark, along with a time, heating and cooling value. If the time is disabled, no check mark, and time displayed in the timefield.text is modified to state: "Disabled". The code works fine when I use reloadData. However, since I'm updated only one row at a time, that's overkill for the big table that I'm using. I'm trying to use reloadRowsAtIndexPaths: but I get screen updates that affect not only the cell that was clicked on, but the previous cell that was clicked on. I was using no animations (UITableViewRowAnimationNone), but changed that to fading animations (UITableViewRowAnimationFade) to see what was going on. Sure enough, the fading occurs on the desired table row and the undesired table row. Also, the time value for the current cell row appears in the previous cell row time field. Once I stay with clicking on one row, there's no problem with the updates, but once I switch to another row, I have both rows affected the one time. It's like the update method is picking up cruft from somewhere.
I have also tried bracketing the reloadRowsAtIndexPaths: call with calls to beginUpdates and endUpdates, and nothing changes.
Clearly I'm not understanding something fundamental here. What am I doing wrong? Here is the method in question:
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)cell.accessoryView;
BOOL checked;
if (intervalDisabled[((indexPath.section * 4) + indexPath.row)] == NO) {
checked = YES;
intervalDisabled[((indexPath.section * 4) + indexPath.row)] = YES;
} else {
checked = NO;
intervalDisabled[((indexPath.section * 4) + indexPath.row)] = NO;
}
UIImage *newImage = (checked) ? [UIImage imageNamed:#"unchecked.png"] : [UIImage imageNamed:#"checked.png"];
[button setBackgroundImage:newImage forState:UIControlStateNormal];
timeField.text = [NSString stringWithFormat:#"%#", [self.dateFormatter stringFromDate:[timeOfDays objectAtIndex:((indexPath.section * 4) + indexPath.row)]]];
//[self.tableView reloadData]; <-- This works fine
NSArray *rowArray = [NSArray arrayWithObject:indexPath];
[self.tableView reloadRowsAtIndexPaths:rowArray withRowAnimation:UITableViewRowAnimationFade];
}
Edit:
OK. Here is code you requested. I hope it helps. I will be glad to post other portions. Sorry, I haven't got the hang for formatting code in this editor yet.
// Customized cell for setpoints
- (UITableViewCell *)setPointsCell:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *temperatureSymbol;
static NSString *kCustomCellID = #"setPointsCellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCustomCellID] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
// Set up the cell.
NSDictionary *dictionary = [listOfRows3 objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"Content"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
//Set properties of label and add its text
cell.textLabel.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
cell.textLabel.text = cellValue;
// Add time field
timeField = [[UITextField alloc] init];
timeField.tag = (indexPath.section * 100) + (indexPath.row * 10) + TIME_FIELD;
CGRect frame = CGRectMake(80.0, 6.0, 90.0, 31.0);
timeField.frame = frame;
timeField.borderStyle = UITextBorderStyleRoundedRect;
timeField.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
// timeField.backgroundColor = [UIColor clearColor];
timeField.textAlignment = UITextAlignmentRight;
// Check and handle situation where time interval is disabled
BOOL rowHasCheck;
if (intervalDisabled[((indexPath.section * 4) + indexPath.row)] == NO) {
rowHasCheck = YES;
timeField.text = [NSString stringWithFormat:#"%#", [self.dateFormatter stringFromDate:[timeOfDays objectAtIndex:((indexPath.section * 4) + indexPath.row)]]];
} else {
rowHasCheck = NO;
timeField.text = #"Disabled";
}
UIImage *image = (rowHasCheck) ? [UIImage imageNamed:#"checked.png"] : [UIImage imageNamed:#"unchecked.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect bFrame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.frame = bFrame; // match the button's size with the image size
[button setBackgroundImage:image forState:UIControlStateNormal];
// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:#selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;
// Override default keyboard handler so we can use a picker instead
timeField.delegate = self;
[cell.contentView addSubview:timeField];
// Set up termperature (F or C) to display
if (temperatureScale == FAHRENHEIT) {
temperatureSymbol = #"F";
} else {
temperatureSymbol = #"C";
}
// Add heating setpoint field
UITextField *heatingSetPointField = [[UITextField alloc] init];
heatingSetPointField.tag = (indexPath.section * 100) + (indexPath.row * 10) + HEATING_FIELD;
frame = CGRectMake(180.0, 6.0, 52.0, 31.0);
heatingSetPointField.frame = frame;
heatingSetPointField.borderStyle = UITextBorderStyleBezel;
heatingSetPointField.backgroundColor = [UIColor colorWithRed:1.0 green:0.2 blue:0.2 alpha:1.0];
heatingSetPointField.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
heatingSetPointField.textAlignment = UITextAlignmentRight;
heatingSetPointField.text = [NSString stringWithFormat:#"%i %#", hSetpoints[((indexPath.section * 4) + indexPath.row)], temperatureSymbol];
// Override default delegate handler
heatingSetPointField.delegate = self;
[cell.contentView addSubview:heatingSetPointField];
// Add cooling setpoint field
UITextField *coolingSetPointField = [[UITextField alloc] init];
coolingSetPointField.tag = (indexPath.section * 100) + (indexPath.row * 10) + COOLING_FIELD;
frame = CGRectMake(240.0, 6.0, 52.0, 31.0);
coolingSetPointField.frame = frame;
coolingSetPointField.borderStyle = UITextBorderStyleBezel;
coolingSetPointField.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
coolingSetPointField.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:1.0 alpha:1.0];
coolingSetPointField.textAlignment = UITextAlignmentRight;
coolingSetPointField.text = [NSString stringWithFormat:#"%i %#", cSetpoints[((indexPath.section * 4) + indexPath.row)], temperatureSymbol];
// Override default delegate handler
coolingSetPointField.delegate = self;
[cell.contentView addSubview:coolingSetPointField];
[timeField release];
[heatingSetPointField release];
[coolingSetPointField release];
return cell;
}
The main problem is that you seem to be using a single timeField reference across all cells. At a minimum, the timeField should be created separately in each cell (just like the heatingSetPointField) and then extract it from the cell later using its tag.
There are many other issues (eg. text fields will get added multiple times to each cell when a cell is reused).
I think reloadData is "working" because you haven't tried scrolling the table view up/down and seeing wrong data in the cells when they come back into view.
You may want to read the Table View Programming Guide (especially the Customizing Cells section) and start with a simpler table view cell and add one layer of complexity at a time until you reach your goal.