Custom tableViewCell example with images and lables - iphone

I am creating custom UITableViewCell before starting to create it i read many articles about it and I start to create my own CustomTableViewCell.
In my custom TableViewCell I have 4 filds:
UILabel* cellTitle
UILabel* cellDateTime
UIView* cellMainImage
UIImageView* arraow image
Here is how is my TableViewCell appear:
And here is the code: of CustomTableViewCell.h
#import <UIKit/UIKit.h>
#define TAGS_TITLE_SIZE 20.0f
#define TITLE_LABEL_TAG 1
#define DATA_TIME_LABEL_TAG 5
#define ARROW_IMAGE_TAG 6
#define MAIN_IMAGE_TAG 7
// Enumeration for initiakization TableView Cells
typedef enum {
NONE_TABLE_CELL = 0,
NEWS_FEED_TABLE_CELL = 1,
TAGS_TABLE_CELL = 2
}TableTypeEnumeration;
// Class for Custom Table View Cell.
#interface CustomTableViewCell : UITableViewCell {
// Title of the cell.
UILabel* cellTitle;
UILabel* cellDataTime;
UIView* cellMainImage;
UIImageView* cellArrowImage;
}
// Set the title of the cell.
- (void) SetCellTitle: (NSString*) _cellTitle;
- (void) SetCellDateTime: (NSString*) _cellDataTime;
- (void) ReleaseCellMainImage;
- (void) InitCellTitleLable;
- (void) InitCellDateTimeLabel;
- (void) InitCellMainImage;
// Init With Style (With additional parametr TableTypeEnumeration)
- (id)initWithStyle: (UITableViewCellStyle)style reuseIdentifier: (NSString *)reuseIdentifier tableType:(TableTypeEnumeration)tabletypeEnum;
#end
And here is the code of: CustomTableViewCell.m
#import "CustomTableViewCell.h"
#implementation CustomTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
return [self initWithStyle:style reuseIdentifier:reuseIdentifier tableType:NONE_TABLE_CELL];
}
- (id)initWithStyle: (UITableViewCellStyle)style reuseIdentifier: (NSString *)reuseIdentifier tableType:(TableTypeEnumeration)tabletypeEnum {
// Get Self.
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Switch table View Cells
switch(tabletypeEnum) {
case NEWS_FEED_TABLE_CELL: {
// Create Cell Title Text
cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(75.0f, 2.5f, 180.0f, 33.0f)];
cellTitle.tag = TITLE_LABEL_TAG;
cellTitle.font = [UIFont boldSystemFontOfSize: 13.0f];
cellTitle.lineBreakMode = UILineBreakModeWordWrap;
cellTitle.numberOfLines = 0;
cellTitle.textAlignment = UITextAlignmentLeft;
cellTitle.textColor = [UIColor blackColor];
[self.contentView addSubview:cellTitle];
[cellTitle release];
// Create Cell Description Text.
cellDataTime = [[UILabel alloc] initWithFrame:CGRectMake(135.0f, 38.0f, 100.0f, 15.0f)];
cellDataTime.tag = DATA_TIME_LABEL_TAG;
cellDataTime.font = [UIFont italicSystemFontOfSize: 12.0f];
cellDataTime.textAlignment = UITextAlignmentLeft;
cellDataTime.textColor = [UIColor blackColor];
cellDataTime.lineBreakMode = UILineBreakModeWordWrap;
[self.contentView addSubview:cellDataTime];
[cellDataTime release];
// Create Cell Arrow Image.
cellArrowImage = [[UIImageView alloc] initWithFrame:CGRectMake(260.0f, 7.0f, 40.0f, 49.0f)];
cellArrowImage.tag = ARROW_IMAGE_TAG;
cellArrowImage.backgroundColor = [UIColor whiteColor];
cellArrowImage.image = [UIImage imageNamed:#"Grey Arrow.png"];;
[self.contentView addSubview:cellArrowImage];
[cellArrowImage release];
// Create Cell Main Image.
cellMainImage = [[[UIView alloc] initWithFrame:CGRectMake(2.0f, 2.5f, 55.0f, 50.0f)] autorelease];
cellMainImage.tag = MAIN_IMAGE_TAG;
[self.contentView addSubview:cellMainImage];
break;
}
case TAGS_TABLE_CELL: {
// Create and initialize Title of Custom Cell.
cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, (44 - TAGS_TITLE_SIZE)/2, 260, 21)];
cellTitle.backgroundColor = [UIColor clearColor];
cellTitle.opaque = NO;
cellTitle.textColor = [UIColor blackColor];
cellTitle.highlightedTextColor = [UIColor whiteColor];
cellTitle.font = [UIFont boldSystemFontOfSize:TAGS_TITLE_SIZE];
cellTitle.textAlignment = UITextAlignmentLeft;
[self.contentView addSubview:cellTitle];
[cellTitle release];
break;
}
default: break;
}
}
return self;
}
- (void) ReleaseCellMainImage {
[cellMainImage release];
}
- (void) InitCellTitleLable {
cellTitle = (UILabel *)[self.contentView viewWithTag:TITLE_LABEL_TAG];
}
- (void) InitCellDateTimeLabel {
cellDataTime = (UILabel *)[self.contentView viewWithTag:DATA_TIME_LABEL_TAG];
}
- (void) InitCellMainImage {
//UIView* oldImage = [self.contentView viewWithTag:MAIN_IMAGE_TAG];
//[oldImage removeFromSuperview];
}
- (void) SetCellTitle: (NSString*) _cellTitle {
cellTitle.text = _cellTitle;
}
- (void) SetCellDateTime: (NSString*) _cellDataTime {
cellDataTime.text = _cellDataTime;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
- (void)dealloc {
// Call base delloc
[super dealloc];
}
#end
Now when I use my CustomTableViewCell in the code of the program the memory of my iphone always go up !!! Every time when I open tableView the memory grows for 2mb and when I open and close tableView for 10times it become more then 30mb !!! Whot can I do ???
And one more question
How I can get the event when user for example press on my image in custom cell ???

In addition to considering cell reuse as others say, if the memory use goes up with each open, you may have a memory leak. Perhaps the view you have that creates the table is not releasing it when deallocated.

You aren't reusing your cells. Hence a new cell is created everytime you scroll.
In your delegate you need to recreate cell as follows:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (nil == cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
return cell;

Are you implementing cell re-use when creating the cell? There is no indication in the code that you attempt to dequeue a reusable cell from UITableView prior to the init, though this may be in the table view itself. As can be seen in Praveens post, there is an attempt to dequeue a cell, and only if this returns nil is the cell being initialised.
As a result, you may be creating a new cell object every time this particular cell comes into view. Is cell reuse adopted in the table view?
What code is in the tableview delegate method - tableView:cellForRowAtIndexPath?

Related

how to hide custom button when editing mode is enabled?

I made a custom button using drawRect: and put it in my headerview for my tableview. I want the custom button to be hidden when editing mode is selected. i know i can do that by using the method:
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
but for some reason, my button isn't actually dissapearing when I either 1) set it to nil, 2) or use the button.hidden property. Here is my code:
TableViewController.h:
#interface ToDoTableViewController : UITableViewController <Properties2ViewControllerDelegate, UITableViewDelegate>{
addButtonView *button;
}
#property (strong, nonatomic) NSMutableArray *taskArray;
#property (strong, nonatomic) NSMutableArray *completedArray;
-(IBAction)addCell:(id)sender;
-(void)buttonPressed:(id)sender;
#end
TableViewController.m
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView;
UIView *seperatorView;
CGRect testFrame = CGRectMake(280.0, 5.0, 30.0, 30.0);
button = [[addButtonView alloc]initWithFrame:testFrame];
NSString *sectionTitle = #"Incomplete Tasks";
NSString *section2Title = #"Completed Tasks";
UILabel *label = [[UILabel alloc]init];
label.textColor = [UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f];
label.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:25];
label.backgroundColor = [UIColor clearColor];
label.frame = CGRectMake(10.0, 0.0, 320.0, 40.0);
headerView = [[UIView alloc]initWithFrame:label.frame];
[button addTarget:self action:#selector(addCell:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
CGRect sepFrame = CGRectMake(0, headerView.frame.size.height-2, 320, 1);
seperatorView = [[UIView alloc] initWithFrame:sepFrame];
seperatorView.backgroundColor = [UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f];
[headerView addSubview:seperatorView];
switch (section) {
case 0:
label.text = sectionTitle;
[headerView addSubview:label];
[headerView addSubview:button];
break;
case 1:
label.text = section2Title;
[headerView addSubview:label];
// if (completedArray == nil)
// headerView.hidden = YES;
break;
}
return headerView;
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
if([self isEditing]){
button.hidden = YES;
}else {
button.hidden = NO;
}
}
---EDIT----
-(void)setEditing:(BOOL)editing animated:(BOOL)animated{
[super setEditing:editing animated:animated];
if([self isEditing]){
button.hidden = YES;
[[self tableView] reloadData]; //shouldn't this make the button dissapear?
}else {
button.hidden = NO;
}
}
Whenever the table view is reloaded your code will recreate the entire header, including creating a new button. So you may have set the old button to hidden and then it gets destroyed and replaced by a new, visible, button.
When returning the header, either create it once and always return the same instance, then changing the hidden property should work. Or, check if the table isEditing each time the header is created and decide what to do as a result.
Let's go with the more efficient header reuse option:
Create a property to hold the header view(s) - an array.
In viewDidLoad create the header views (using your current code, but moved to a different method).
Add the header views to the property (ensure you initialise the array first)
Change viewForHeaderInSection to just return the header from the array (based on section)
In setEditing: iterate through the array and hide/show each button
(You could store the buttons in another array to make that easy)

How to swipe tableviewcell for particular row in a section

I have a custom UITableview which is diveded into sections, I have implemented UISwipeGestureRecognizer into it. When I swipe my table view cell, a UIButton appears. The problem I am now facing is when I swipe the first table view cell, another cell in successive sections also recognizes the swipe gesture. I am not able to find how to swipe a cell of particular a section without other cells of other sections getting swiped.
I just want to swipe, I don't want to delete/insert or add check marks.
UPDATE.... This is my customCell.m
Tableview = customTableView
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
self.symptomCellImageView.contentMode=UIViewContentModeScaleToFill;
swipeButton = [[UIButton alloc]init];
swipeButton .frame = CGRectMake(220.0, 8.0, 30.0, 30.0);
swipeButton .hidden = NO;
}
In my MainViewController.m
-(void)viewDidLoad
{
symptomSwipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandleLeft)];
symptomSwipeLeft .numberOfTouchesRequired = 1;
symptomSwipeLeft .direction = UISwipeGestureRecognizerDirectionLeft;
[customTableView addGestureRecognizer:symptomSwipeLeft];
[self addGestureRecognizer:symptomSwipeRight];
symptomSwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeHandleRight)];
symptomSwipeRight.numberOfTouchesRequired = 1;
symptomSwipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[customTableView addGestureRecognizer:symptomSwipeRight];
[self addGestureRecognizer:symptomSwipeRight];
}
- (void)swipeHandleLeft:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer
{
CGPoint location = [aSwipeGestureRecognizer locationInView:customTableView ];
NSIndexPath * indexPath = [customTableView indexPathForRowAtPoint:location];
if(indexPath)
{
UITableViewCell * cell = (UITableViewCell *)[customTableView cellForRowAtIndexPath:indexPath];
[cell.swipeButton addTarget:self action:#selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside];
}
}
- (void)swipeHandleRight:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer
{
CGPoint location = [aSwipeGestureRecognizer locationInView:customTableView ];
NSIndexPath * indexPath = [customTableViewindexPathForRowAtPoint:location];
if(indexPath)
{
UITableViewCell * cell = (UITableViewCell *)[customTableView cellForRowAtIndexPath:indexPath];
[cell.swipeButton addTarget:self action:#selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside];
}
}
Try:
- (void)handleSwipe:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer; {
CGPoint location = [aSwipeGestureRecognizer locationInView:_tableView];
NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];
if(indexPath){
UITableViewCell * cell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];
[cell whateverMethodYouWant];
}
}
As a side note, the reason you are getting calls to multiple cells is because the row is not unique enough. There is a row = 0 in all of you sections. Therefore, if you want each cell to have a unique number attached to its .tag property, you would need to know the largest number of rows in any section (call it largestNumberOfRows), and then compute:
cell.swipeButton.tag = (indexPath.section * largestNumberOfRows) + indexPath.row;
Hope that helps!
EDIT:
To use the above method, in your viewDidLoad; method, add the following code:
UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)];
[_tableView addGestureRecognizer:recognizer];
EDIT 2
Looking at your code, you have put your gesture recognizers in the wrong file. Here is the setup:
In your MainViewController.m file:
- (void)viewDidLoad; {
[super viewDidLoad];
UISwipeGestureRecognizer * recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(handleSwipe:)];
[recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft)];
[_tableView addGestureRecognizer:recognizer];
}
- (void)handleSwipe:(UISwipeGestureRecognizer *)aSwipeGestureRecognizer; {
CGPoint location = [aSwipeGestureRecognizer locationInView:_tableView];
NSIndexPath * indexPath = [_tableView indexPathForRowAtPoint:location];
if(indexPath){
UITableViewCell * cell = (UITableViewCell *)[_tableView cellForRowAtIndexPath:indexPath];
if(aSwipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight){
[cell symptomCellSwipeRight];
}
else if(aSwipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionLeft){
[cell symptomCellSwipeLeft];
}
}
}
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath; {
// Initial your cell. Then add:
[cell.swipeButton addTarget:self action:#selector(secondPageButton:) forControlEvents:UIControlEventTouchUpInside];
}
In your PPsymptomTableCell.m file:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier; {
if((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])){
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
self.symptomCellImageView.contentMode=UIViewContentModeScaleToFill;
// SHARE ON FACEBOOK BUTTON //
shareFacebookButton = [[UIButton alloc]init];
shareFacebookButton.frame = CGRectMake(220.0, 8.0, 30.0, 30.0);
shareFacebookButton.hidden = NO;
// DISPLAY NOTIFICATION IMAGE //
selectedCellImageDisplay = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"selectedSymptomImage.png"]];
selectedCellImageDisplay.frame = CGRectMake(240.0, 8.0, 30.0, 30.0);
// SYMPTOM NAME //
symptomCellLabel=[[UILabel alloc]initWithFrame:CGRectMake(15.0,0.0 ,280.0,40.0)];
symptomCellLabel.font=[UIFont fontWithName:#"Rockwell" size:17];
symptomCellLabel.textColor=[UIColor blackColor];
symptomCellLabel.backgroundColor=[UIColor clearColor];
[self.contentView addSubview:symptomCellLabel];
[self.contentView addSubview:selectedCellImageDisplay];
[self.contentView addSubview:shareFacebookButton];
}
return self;
}
- (void)symptomCellSwipeLeft; {
[UIView beginAnimations:#"HideView" context:nil];
symptomCellLabel.frame = CGRectMake(-183.0, 0.0, 280.0, 40.0);
selectedCellImageDisplay.frame = CGRectMake(-340.0, 8.0, 30.0, 30.0);
shareFacebookButton.frame = CGRectMake(120.0, 8.0, 80.0, 30.0);
shareFacebookButton.hidden = NO;
shareFacebookButton.backgroundColor = [UIColor redColor];
[UIView setAnimationDuration:0.3f];
[UIView commitAnimations];
}
- (void)symptomCellSwipeRight; {
[UIView beginAnimations:#"HideView" context:nil];
symptomCellLabel.frame = CGRectMake(15.0,0.0 ,280.0,40.0);
selectedCellImageDisplay.frame = CGRectMake(240.0, 8.0, 30.0, 30.0);
shareFacebookButton.frame = CGRectMake(220.0, 8.0, 30.0, 30.0);
shareFacebookButton.hidden = YES;
[UIView setAnimationDuration:0.3f];
[UIView commitAnimations];
}
This should get everything working nicely.
You are implementing your table with sections, then make your tag value of combination of indexPath.sections and indexPath.row.
But make sure you are implementing without overlapping of tagValue.

App crashes on releasing custom Table Cell

i have a problem with my app crashing when my custom TableViewCell gets released.
the Cell gets initialized like the following in cellForRowAtIndexPath:
SearchTableViewCell *cell = (SearchTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[SearchTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
cell.nameLabel.text = #"some text";
cell.addressLabel.text = #"some more text";
the cell class itself looks like this
#import <UIKit/UIKit.h>
#class EGOImageView;
#interface SearchTableViewCell : UITableViewCell {
UILabel *nameLabel;
UILabel *addressLabel;
EGOImageView *imageView;
}
#property (nonatomic, retain) UILabel *nameLabel;
#property (nonatomic, retain) UILabel *addressLabel;
- (UILabel *)labelWithColor:(UIColor*)color selectedColor:(UIColor*)selectedColor fontSize:(CGFloat)fontSize bold:(BOOL)bold frame:(CGRect)rect;
- (void)setThumb:(NSString*)thumb;
#end
.m
#import "SearchTableViewCell.h"
#import "EGOImageView.h"
#import "UIView+Additions.h"
#implementation SearchTableViewCell
#synthesize nameLabel = _nameLabel, addressLabel = _addressLabel;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
// Initialization code
UIView *myContentView = self.contentView;
// Name
_nameLabel = [self labelWithColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:16.0f bold:YES frame:CGRectMake(140.0f, 16.0f, 181.0f, 21.0f)];
[myContentView addSubview:_nameLabel];
[_nameLabel release];
// Adress
_addressLabel = [self labelWithColor:[UIColor blackColor] selectedColor:[UIColor whiteColor] fontSize:13.0f bold:YES frame:CGRectMake(140.0f, _nameLabel.bottom, 181.0f, 21.0f)];
[myContentView addSubview:_addressLabel];
[_addressLabel release];
// Image
imageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:#"placeholder.png"]];
imageView.frame = CGRectMake(9.0f, 9.0f, 120.0f, 80.0f);
[myContentView addSubview:imageView];
[imageView release];
}
return self;
}
- (UILabel *)labelWithColor:(UIColor*)color selectedColor:(UIColor*)selectedColor fontSize:(CGFloat)fontSize bold:(BOOL)bold frame:(CGRect)rect {
UIFont *font;
if(bold) {
font = [UIFont boldSystemFontOfSize:fontSize];
} else {
font = [UIFont systemFontOfSize:fontSize];
}
UILabel *label = [[UILabel alloc] initWithFrame:rect];
label.backgroundColor = [UIColor clearColor];
label.textColor = color;
label.highlightedTextColor = selectedColor;
label.font = font;
return label;
}
- (void)setThumb:(NSString*)thumb {
imageView.imageURL = [NSURL URLWithString:thumb];
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];
if(!newSuperview) {
[imageView cancelImageLoad];
}
}
- (void)dealloc {
[_addressLabel release];
[_nameLabel release];
[imageView release];
[super dealloc];
}
#end
does anybody have an idea why my app crashes on releasing such a cell? commenting out the 2 labels and the image view on dealloc method, the app doesn't crash, but then there will be a memory leak right?
thanks for all hints! please leave a comment if something is unclear!
imageView is being released twice, once when it is created:
imageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:#"placeholder.png"]];
imageView.frame = CGRectMake(9.0f, 9.0f, 120.0f, 80.0f);
[myContentView addSubview:imageView];
[imageView release];
and once in dealloc:
- (void)dealloc {
[_addressLabel release];
[_nameLabel release];
[imageView release];
[super dealloc];
}
You already release the memory for that labels after adding to view,again you are trying to release memory those objects are already relese in dealloc method that's why it is killing.if you remove 3 statements in dealloc method it will not crash.
I think the Problem is that you release your properties direct in the initWithStyle function and additional to that in dealloc again. Try removing the release from initWithStyle:
Furthermore you named your variables without _ in the interface but with _ #synthesize'ing in the implementation

Scroll View in Grid TableView

I'm completely new to iPhone development. I have a query regarding how to implement scroll view in table view. I'm using following code
#import <UIKit/UIKit.h>
#class ScrollViewViewController;
#interface ScrollViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ScrollViewViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ScrollViewViewController *viewController;
#end
////////////////////////////////////////////
#import "ScrollViewAppDelegate.h"
#import "ScrollViewViewController.h"
#implementation ScrollViewAppDelegate
#synthesize window;
#synthesize viewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
///////////////////////////
#import <UIKit/UIKit.h>
#interface MyTableCell : UITableViewCell {
NSMutableArray *columns;
}
- (void)addColumn:(CGFloat)position;
#end
//////////////////////////
#import "MyTableCell.h"
#implementation MyTableCell
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
// Initialization code
columns = [NSMutableArray arrayWithCapacity:5];
[columns retain];
}
return self;
}
- (void)addColumn:(CGFloat)position {
[columns addObject:[NSNumber numberWithFloat:position]];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
// just match the color and size of the horizontal line
CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
CGContextSetLineWidth(ctx, 0.25);
for (int i = 0; i < [columns count]; i++) {
// get the position for the vertical line
CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue];
CGContextMoveToPoint(ctx, f, 0);
CGContextAddLineToPoint(ctx, f, self.bounds.size.height);
}
CGContextStrokePath(ctx);
[super drawRect:rect];
}
- (void)dealloc {
[super dealloc];
[columns dealloc];
}
#end
//////////////////////
#import <UIKit/UIKit.h>
#interface RootViewController : UITableViewController {
}
#end
/////////////////
#import "RootViewController.h"
#import "MyTableCell.h"
#implementation RootViewController
#define LABEL_TAG 1
#define VALUE_TAG 2
#define FIRST_CELL_IDENTIFIER #"TrailItemCell"
#define SECOND_CELL_IDENTIFIER #"RegularCell"
- (void)viewDidLoad {
// Add the following line if you want the list to be editable
// self.navigationItem.leftBarButtonItem = self.editButtonItem;
self.title = #"Grids!";
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
return 19;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = [NSString stringWithFormat:#"MyIdentifier %i", indexPath.row];
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 30.0,
tableView.rowHeight)] autorelease];
[cell addColumn:40];
label.tag = LABEL_TAG;
label.font = [UIFont systemFontOfSize:12.0];
label.text =#"S.NO";// [NSString stringWithFormat:#"%d", indexPath.row];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor redColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
label = [[[UILabel alloc] initWithFrame:CGRectMake(40.0, 0, 70.0,
tableView.rowHeight)] autorelease];
[cell addColumn:120];
label.tag = VALUE_TAG;
label.font = [UIFont systemFontOfSize:12.0];
// add some silly value
label.text =#"Product ID";// [NSString stringWithFormat:#"%d", indexPath.row * 4];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
label = [[[UILabel alloc] initWithFrame:CGRectMake(134.0, 0, 70.0,
tableView.rowHeight)] autorelease];
[cell addColumn:220];
label.tag = VALUE_TAG;
label.font = [UIFont systemFontOfSize:12.0];
// add some silly value
label.text =#"Product Name";// [NSString stringWithFormat:#"%d", indexPath.row * 4];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor greenColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
label = [[[UILabel alloc] initWithFrame:CGRectMake(230.0, 0, 70.0,
tableView.rowHeight)] autorelease];
[cell addColumn:310];
label.tag = VALUE_TAG;
label.font = [UIFont systemFontOfSize:12.0];
// add some silly value
label.text =#"Customer Name";// [NSString stringWithFormat:#"%d", indexPath.row * 4];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor greenColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
label = [[[UILabel alloc] initWithFrame:CGRectMake(320.0, 0, 70.0,
tableView.rowHeight)] autorelease];
[cell addColumn:400];
label.tag = VALUE_TAG;
label.font = [UIFont systemFontOfSize:12.0];
// add some silly value
label.text =#"Customer Product";// [NSString stringWithFormat:#"%d", indexPath.row * 4];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor greenColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
}
- (void)viewDidDisappear:(BOOL)animated {
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[super dealloc];
}
#end
////////////
#import <UIKit/UIKit.h>
#interface ScrollViewViewController : UIViewController<UIScrollViewDelegate> {
}
#end
/////////////
#import "ScrollViewViewController.h"
#import "RootViewController.h"
#implementation ScrollViewViewController
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
RootViewController *RootViewControllerLink = [[RootViewController alloc]initWithNibName:#"RootViewController" bundle:nil];
RootViewControllerLink.view.tag = 100;
/* UIImageView *imgView = [[[UIImageView alloc] initWithImage:
[UIImage imageNamed:#"winkler-gnu-blue.png"]] autorelease];
imgView.tag = 100;
*/
UIScrollView *scrollView = [[[UIScrollView alloc]
initWithFrame:CGRectMake(0,0,320,480)] autorelease];
scrollView.delegate = self;
scrollView.minimumZoomScale = 0.25;
scrollView.maximumZoomScale = 2;
scrollView.bounces = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.contentSize = RootViewControllerLink.view.frame.size;
scrollView.contentOffset =
CGPointMake((RootViewControllerLink.view.frame.size.width-320)/2,
(RootViewControllerLink.view.frame.size.height-480)/2);
[scrollView addSubview:RootViewControllerLink.view];
self.view = scrollView;
}
/*- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return [self.view viewWithTag:100];
}
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
return YES;
}// default returns YES
- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
return YES;
}
*/
// not called if canCancelContentTouches is NO. default returns YES if view isn't UIControl
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
In above code if I set scroll for UIIMage then it works but if I set scroll view for RootViewController then it doesn't work.
I didn't read your code, please reformat it so others can read it easily.
What do you mean by a UIScrollView in an UITableView? Inside the cells? Still I don't get it.
FYI UITableView inherits from UIScrollView ...
What functionality do you exactly want to achieve?
I will recommend to read some of the samples given by Apple. There are very good and extensive examples specially regarding UIKit.

Custom UITableViewCell won't redraw on setNeedsDisplay

I created a custom UITableViewCell class with a UIButton, a UIImage, and two UILabels. The button and the image are overlayed on top of each other, and only one is displayed at a time. The expected behavior is you touch on the button, the button disappears, and it displays the image. The UITableViewCells are set to be reused. Here's my code:
Constructor:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
unheartButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
unheartButton.backgroundColor = [UIColor clearColor];
unheartButton.frame = CGRectMake(10, 13, 20, 18);
[unheartButton addTarget:self action:#selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[unheartButton setBackgroundImage:[UIImage imageNamed:#"redheart.png"] forState:UIControlStateNormal];
imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(12, 13, 16, 16);
NSMutableArray *array = [[NSMutableArray alloc] init];
for (int ndx = 1; ndx < 13; ndx++) {
[array addObject:[UIImage imageNamed:[NSString stringWithFormat:#"icon-loading-%d (dragged).tiff", ndx]]];
}
imageView.animationImages = array;
imageView.animationDuration = 1;
[array release];
[self.contentView addSubview:imageView];
[self.contentView addSubview:unheartButton];
return self;
}
}
- (void) setModel:(MyModel *)myModel {
model = myModel;
if (model.hideImage) {
imageView.hidden = YES;
unheartButton.hidden = NO;
else {
imageView.hidden = NO;
unheartButton.hidden = YES;
}
}
Button click:
- (IBAction) onButtonClick: (id) sender {
model.hideImage = NO;
unheartButton.hidden = YES;
imageView.hidden = NO;
[imageView startAnimating];
[self setNeedsDisplay];
[self.contentView setNeedsDisplay];
[self.unheartButton setNeedsDisplay];
[self.imageView setNeedsDisplay];
}
I'm calling setNeedsDisplay on everything, but nothing seems to happen. If I scroll off the screen and back up, the button is hidden and now the loading icon is shown, but this only happens after a scroll. I'm not sure what I need to do to get the cell to repaint.
The UITableView does some fancy caching to support scrolling and the like; I've had similar issues with refreshing a specific cell when I use custom views.
You can use reloadRowsAtIndexPaths:withRowAnimation: to make the table reload just that row. See this post for more information: Why won't my UITableViewCell deselect and update its text?