I'm setting the placeholders for a cell inside the if(cell == nil) condition.
Still, when I scroll my table, The placeholder values get interchanged. Please help on this.
Here is the code from CellForRowAtIndexpath: Please let me know where I'm mistaking..
static NSString *cellIdentifierCell = #"MTRegisterTableViewCell";
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:[MTUtility
setXibWithName:cellIdentifierCell] owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
if(indexPath.row==6)
{
if (cell.txtRegister.text.length == 0) {
if (cell.txtRegister.placeholder.length == 0) {
cell.txtRegister.placeholder=[self.profileTitlesArray
objectAtIndex:indexPath.row-1];
}
}
NSLog(#"Placeholder :%# && iINDEXPATH.ROW
:%ld",cell.txtRegister.placeholder,(long)indexPath.row);
}
else
{
if (cell.txtRegister.text.length == 0) {
if (cell.txtRegister.placeholder.length == 0) {
cell.txtRegister.placeholder=[self.profileTitlesArray
objectAtIndex:indexPath.row];
}
}
}
}
if(indexPath.row==5)
{
static NSString *cellIdentifierCell = #"MTRegisterDropdownCell";
MTRegisterDropdownCell *cellDrop = [tableView
dequeueReusableCellWithIdentifier:cellIdentifierCell];
if (cellDrop == nil)
{
NSArray *nib;
nib = [[NSBundle mainBundle] loadNibNamed:[MTUtility
setXibWithName:cellIdentifierCell] owner:self options:nil];
cellDrop = (MTRegisterDropdownCell *)[nib objectAtIndex:0];
[cellDrop configureCell:nil];
}
self.countryCell=cellDrop;
cellDrop.clipsToBounds=NO;
self.tblDropdown.clipsToBounds=NO;
[cellDrop.btnCountry addTarget:self action:#selector(btnCountryClicked:)
forControlEvents:UIControlEventTouchUpInside];
[cellDrop.btnState addTarget:self action:#selector(ButtonStateClicked:)
forControlEvents:UIControlEventTouchUpInside];
if(self.strSelectedCountry)
{
[cellDrop.txtCountry setText:self.strSelectedCountry];
}
else{
if ([self.countriesArray containsObject:kDefaultCountry]) {
[cellDrop.txtCountry setText:kDefaultCountry];
NSInteger index = [self.countriesArray
indexOfObject:kDefaultCountry];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index
inSection:0];
self.tblDropdown.tag=TABLECOUNTRYTAG;
[self tableView:self.tblDropdown didSelectRowAtIndexPath:indexPath];
}
}
if(self.strSelectedState)
{
[cellDrop.txtState setText:self.strSelectedState];
}
return cellDrop;
}
else
{
if (cell.txtRegister.text.length == 0) {
NSLog(#"Placeholder :%#",cell.txtRegister.placeholder);
if ([cell.txtRegister.placeholder isEqualToString:#"City"]) {
if (self.strSelectedCity) {
cell.txtRegister.text = self.strSelectedCity;
}
else
cell.txtRegister.text=[self.profileTitlesArray
objectAtIndex:indexPath.row];
NSLog(#"profileTitlesArray :%#",self.profileTitlesArray);
NSLog(#"indexpath.Row :%ld && cell TEXT :%#",
(long)indexPath.row,cell.txtRegister.text);
BaseButton *aBtn = [[BaseButton alloc]
initWithFrame:CGRectMake(cell.txtRegister.frame.origin.x-50,
cell.txtRegister.frame.origin.y-10, cell.txtRegister.frame.size.width+50,
cell.txtRegister.frame.size.height+50)];
[aBtn setBackgroundColor:[UIColor clearColor]];
[cell.txtRegister addSubview:aBtn];
[aBtn addTarget:self action:#selector(btnCitiesClicked:)
forControlEvents:UIControlEventTouchUpInside];
self.cityCelll = cell;
cell.clipsToBounds = NO;
}
}
else{
if ([cell.txtRegister.placeholder isEqualToString:#"City"]) {
if (self.strSelectedCity) {
cell.txtRegister.text = self.strSelectedCity;
}
}
}
}
[cell configureCell:registerModel];
return cell;
}
Did not read you whole code, but it sounds like you'll need to reset the placeholders before a cell gets reused, they are not necessarly recreated on scroll, that is the point of a table view after all.
See -[UITableViewCell prepareForReuse]. It is called upon dequeue / reuse an existing table cell, so technically when you call -[UITableView dequeueReusableCellWithIdentifier:] (You'll need to subclass UITableViewCell, it organizes your code further anyway).
I finally solved it by using following line:
NSString *cellIdentifierCell = [NSString
stringWithFormat:#"MTRegisterTableViewCell %ld",(long)indexPath.row];
cell = (MTRegisterTableViewCell *)[tableView
dequeueReusableCellWithIdentifier:cellIdentifierCell];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:[MTUtility
setXibWithName:#"MTRegisterTableViewCell"] owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
Related
I am using the following code to have a tab view. When you click one tab it expands downwards, this is currently working okay but if I open the first tab with the indexPath of 0 which should load a separate custom opened view it will load but then when I go to the other indexPath tabs it displays the same view but if I reload the app and go to the other tabs first they load the correct view. It's nearly like the app is making a cache?
Is there any way to solve this??
Thanks
- (void)viewDidLoad {
row = 1000;
NSLog(#"row is %d", row);
[super viewDidLoad];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
infoStory = [userDefaults objectForKey:#"storyname"];
// Do any additional setup after loading the view.
numbers = [NSArray arrayWithObjects:#"1", #"2", #"3", #"4", #"5", #"6", #"7", #"8", nil];
// Initialize thumbnails
text = [NSArray arrayWithObjects: #"Arriving by Bus or Train", #"Arriving by Car (Autobahn 5)", #"Arriving by Car (Autobahn 6)", #"Wifi", #"Registration", #"Refreshments", #"Evening Event", #"Contact Information", nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(row == indexPath.row){
if(indexPath.row == 0){
static NSString *simpleTableIdentifier2 = #"TableViewCellOPENEDbustaxi2";
TableViewCell1 *cell = (TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableViewCellOPENEDbustaxi2" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.text.text = [text objectAtIndex:indexPath.row];
return cell;
}
else{
static NSString *simpleTableIdentifier = #"TableViewCellOPENED";
TableViewCell1 *cell2 = (TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell2 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableViewCellOPENED" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
cell2.text.text = [text objectAtIndex:indexPath.row];
return cell2;
}
}
else{
static NSString *simpleTableIdentifier = #"TableViewCellINFO";
TableViewCell1 *cell = (TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableViewCellINFO" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.text.text = [text objectAtIndex:indexPath.row];
return cell;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [numbers count];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(row == indexPath.row){
row = 1000;
NSLog(#"row is %d", row);
[self.tableViewObject reloadData];
}
else{
row = indexPath.row;
NSLog(#"row is %d", row);
[self.tableViewObject reloadData];
}
}
#end
I think the problem might be that you did not specify the correct reuse identifier in the attributes inspector of the cell in your XIB file(s). You should check if you use the wrong one somewhere, so dequeueing will return an unexpected cell.
You could also set the appropriate reuseIdentifier manually, with a little setValue:forKey: trick:
static NSString *simpleTableIdentifier2 = #"TableViewCellOPENEDbustaxi2";
TableViewCell1 *cell = (TableViewCell1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"TableViewCellOPENEDbustaxi2" owner:self options:nil];
cell = [nib objectAtIndex:0];
[cell setValue:simpleTableIdentifier2 forKey:#"reuseIdentifier"];
}
and so on for the other cells and identifiers.
I have a small issue with the multiple CustomCells by using SegmentedControl. I have imported a framework HMSegmentedControl with 4 segmentedcontrols like (Admin, Engineer, Doctor, Employee). Now the problem is how to use multiple custom cells like (AdminCellList, EngineerCellList, DoctorCellList, EmployeeListCell). When Admin SegmentedControl is Clicked AdminCellList should be loaded and so on. Following is what i have tried. TIA.
MYViewController.h
#interface MYViewController : UIViewController
{
NSUInteger currentScreen;
}
MYViewController.m
self.AdminView.hidden = NO;
self.EngineerView.hidden = YES;
self.DoctorView.hidden = YES;
self.EmployeeView.hidden = YES;
currentScreen=0;
[self.scrollView addSubview:self.AdminView];
self.edgesForExtendedLayout = UIRectEdgeNone;
CGFloat viewWidth = CGRectGetWidth(self.view.frame);
HMSegmentedControl *segmentedControl = [[HMSegmentedControl alloc] initWithSectionTitles:#[#"Admin", #"Engineer", #"Doctor", #"Employee"]];
segmentedControl.frame = CGRectMake(0, 0, viewWidth, 45);
segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleFullWidthStripe;
segmentedControl.backgroundColor = [UIColor colorWithRed:170/255.0 green:170/255.0 blue:170/255.0 alpha:1.0];
segmentedControl.selectionIndicatorColor = [UIColor blackColor];
segmentedControl.selectionIndicatorHeight = 2.0f;
segmentedControl.verticalDividerEnabled = YES;
segmentedControl.verticalDividerColor = [UIColor blackColor];
segmentedControl.verticalDividerWidth = 1.0f;
segmentedControl.borderColor = [UIColor blackColor];
[segmentedControl setTitleFormatter:^NSAttributedString *(HMSegmentedControl *segmentedControl, NSString *title, NSUInteger index, BOOL selected) {
NSAttributedString *attString = [[NSAttributedString alloc] initWithString:title attributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
return attString;
}];
[segmentedControl addTarget:self action:#selector(segmentedControlChangedValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segmentedControl];
}
- (void)segmentedControlChangedValue:(UISegmentedControl *)segment
{
if(segment.selectedSegmentIndex == 0) {
currentScreen=0;
//Loading Service
}
else if(segment.selectedSegmentIndex == 1) {
currentScreen=1;
//Loading Service
}
else if(segment.selectedSegmentIndex == 2) {
currentScreen=2;
//Loading Service
}
else if(segment.selectedSegmentIndex == 3) {
currentScreen=3;
//Loading Service
}
}
- (void)uisegmentedControlChangedValue:(UISegmentedControl *)segmentedControl {
NSLog(#"Selected index %ld", (long)segmentedControl.selectedSegmentIndex);
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
AdminCellList *cell = (AdminCellList *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"AdminCellList" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (AdminCellList *) currentObject;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
Everything looks good, just need some additions:
1.Reload table each time user changes segment.
2.Put check inside cellForRowAtIndexPath according to currentScreen and load corresponding cell with custom class.
- (void)segmentedControlChangedValue:(UISegmentedControl *)segment {
if(segment.selectedSegmentIndex == 0) {
currentScreen=0;
}
else if(segment.selectedSegmentIndex == 1) {
currentScreen=1;
}
....... more code
[myTableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
if (currentScreen == 0) {
AdminCellList *cell = (AdminCellList *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"AdminCellList" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (AdminCellList *) currentObject;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
}
}
} else if (currentScreen == 1) {
//Same code for cell as AdminCellList
EngineerCellList *cell = (EngineerCellList *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"EngineerCellList" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
cell = (EngineerCellList *) currentObject;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
}
}
} else if (currentScreen == 2) {
//Similar custom cell code here as above
} else {
//Similar custom cell code here as above
}
}
1. in my method in write code example given below -
(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
static NSString *simpleTableIdentifier2 = #"SimpleTableCell2";
if (indexPath.row== 0) {
SimpleTableCell *cell = nil;
cell=(SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
}
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (indexPath.row == 1) {
SimpleTableCell2 *cell2 = nil;
cell2=(SimpleTableCell2 *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
}
if (cell2 == nil)
{
NSArray *nib2 = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell2" owner:self options:nil];
cell2 = [nib2 objectAtIndex:0];
}
cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
cell.prepTimeLabel.text = #"detail";// [prepTime objectAtIndex:indexPath.row];
//cell.imageView.image = [UIImage imageNamed:#"creme_brelee.jpg"];
cell2.nameLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
can any one help me am i write correct code becoz it will give an error
the error is use of undeclear identifier cell and cell2
i dont understand what is this error
it gives the error after making cell2 before cell2 it works perfectly
help me
thanks
It is giving the error because you have declared it like this:
if (indexPath.row== 0) {
SimpleTableCell *cell = nil;
cell=(SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
}
and you are using the cell variable outside the local scope of this condition.
Do this:
SimpleTableCell *cell = nil;
if (indexPath.row== 0)
{
cell=(SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
}
Also make sure that you have imported the header SimpleTableCell.h.
Hope that helps!
if (indexPath.row == 1) {
SimpleTableCell2 *cell2 = nil;
cell2=(SimpleTableCell2 *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
//This is ok
cell2.nameLabel.text = [tableData objectAtIndex:indexPath.row];
}
//No you cant access outside
cell2.nameLabel.text = [tableData objectAtIndex:indexPath.row];
Here, you have declared cell2 inside the if block. So you can use this variable in this scope only. When you try to access cell2 outside, you will get use of undeclared error.
Undeclared identifier means that you didn't register cell class.
You should do that in viewDidLoad:
-(void)viewDidLoad
{
[self.tableView registerClass:[SimpleTableCell class] forCellReuseIdentifier:#"SimpleTableCell"];
[self.tableView registerClass:[SimpleTableCell2 class] forCellReuseIdentifier:#"SimpleTableCell2"];
}
However, NSStringFromClass approach is better when dealing with cell identifiers:
-(void)viewDidLoad
{
[self.tableView registerClass:NSStringFromClass([SimpleTableCell class]) forCellReuseIdentifier:#"SimpleTableCell"];
[self.tableView registerClass:NSStringFromClass([SimpleTableCell2 class]) forCellReuseIdentifier:#"SimpleTableCell2"];
}
If you didn't register a cell class, dequeueReusableCellWithIdentifier can return nil, as specified in the docs:
This method dequeues an existing cell if one is available or creates a new one
using the class or nib file you previously registered. If no cell is available
for reuse and you did not register a class or nib file, this method returns nil.
Additional note: keep your view controller methods as slim as possibile.
you could try these codes:
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath (NSIndexPath *)indexPath{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
static NSString *simpleTableIdentifier2 = #"SimpleTableCell2";
if (indexPath.row== 0) {
SimpleTableCell *cell = nil;
cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil){
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
cell.prepTimeLabel.text = #"detail";// [prepTime objectAtIndex:indexPath.row];
//cell.imageView.image = [UIImage imageNamed:#"creme_brelee.jpg"];
return cell;
}
SimpleTableCell2 *cell2 = nil;
if (indexPath.row == 1) {
cell2 = (SimpleTableCell2 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
if (cell2 == nil){
NSArray *nib2 = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell2" owner:self options:nil];
cell2 = [nib2 objectAtIndex:0];
}
cell2.nameLabel.text = [tableData objectAtIndex:indexPath.row];
}
return cell2;
}
if (indexPath.row== 0) {
SimpleTableCell *cell = nil;
cell=(SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
return cell;
}
if (indexPath.row == 1) {
SimpleTableCell2 *cell2 = nil;
cell2=(SimpleTableCell2 *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier2];
if (cell2 == nil)
{
NSArray *nib2 = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell2" owner:self options:nil];
cell2 = [nib2 objectAtIndex:0];
}
return cell2;
}
UITableViewCell *commonCell = [tableView dequeueReusableCellWithIdentifier:#"commonToALL"];
if (commonCell == nil) {
commonCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"commonToALL"];
}
return commonCell;
Try This. This will create two specific cell for index 0 and 1 while two general Cells for remainder as you have 4 rows.
You'd better check you custom cell's tag in SimpleTableCell.xib
whether the tag is 0
and just one custom cell could work like:
(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.nameLabel.text = [tableData objectAtIndex:indexPath.row];
cell.thumbnailImageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];
cell.prepTimeLabel.text = #"detail";// [prepTime objectAtIndex:indexPath.row];
//cell.imageView.image = [UIImage imageNamed:#"creme_brelee.jpg"];
if (indexPath.row == 1) {
[cell.thumbnailImageView.image setHidden:Yes];
[cell.prepTimeLabel.text setHidden:Yes];
}
return cell;
}
How wrong could it be the code bellow:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
NSArray *array = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil];
for (id currentObject in array) {
if ([currentObject isKindOfClass:[self class]]) {
self = [currentObject retain];
}
}
return self;
}
I'm trying to bring up a custom UITableViewCell from nib file, the problem is that Leaks Instruments says there's a leak inside:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CPVideoGenTVCell *cell = (CPVideoGenTVCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CPVideoGenTVCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.delegate = self;
cell.titleLbl.textColor = [UIColor colorWithRed:0.992 green:0.933 blue:0.0 alpha:1.0];
}
CPTempVideo *aVideo = [self.videos objectAtIndex:indexPath.row];
cell.seasonLbl.text = [NSString stringWithFormat:#"Season %#", [self romanNumberFromArabic:aVideo.season]];
cell.titleLbl.text = [NSString stringWithFormat:#"%2i - \"%#\"",aVideo.idVideo, aVideo.chapterName];
cell.managedImg.url = aVideo.thumbnail;
[[CPDataManager sharedDataManager] manageImage:cell.managedImg];
return cell;
}
think the problem is with retain, but don't understand why are you doing this crazy stuff with initWithStyle..
if ([currentObject isKindOfClass:[self class]]) {
self = currentObject;
}
There's an alternative I've found:
+ (id)cellWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
NSArray *array = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil];
for (id currentObject in array) {
if ([currentObject isKindOfClass:[self class]]) {
return currentObject;
}
}
return nil;
}
just return an autoreleased object and take care of that inside cellForRowAtIndexPath:
CPTempHeadCell *headCell = (CPTempHeadCell *)[tableView dequeueReusableCellWithIdentifier:HeadCellIdentifier];
if (headCell == nil) {
headCell = [CPVideoGenTVCell cellWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
i need two kinds of UITableViewCell that has different height:
a) One with a small UIImageView holding user picture, 3 UILabel's and one UIImageView of 200 x 100.
b) The other has the same like a) without the UIImageView.
Well, in order to achieve this i have built two custom UITableViewCell's and set my UITableView to use them.
The problem is when i scroll the table because sometimes it isn't smooth.
My code is the following (i only post the cellForRowAtIndexPath)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (.....) return HEIGHT1
else return HEIGHT2
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CheckinCellIdentifier";
static NSString *TextCellIdentifier = #"CheckinTextCellIdentifier";
CheckIn *checkin = [self.checkinArray objectAtIndex:indexPath.row];
if (checkin.photoUrl) {
CheckinUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CheckinUITableViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[CheckinUITableViewCell class]]) {
cell = (CheckinUITableViewCell *)currentObject;
if (self.userImage)
cell.userImage.image = self.userImage;
break;
}
}
}
cell.checkin = checkin;
UIImage *imageCheckin = [self.checkinImages objectForKey:[NSNumber numberWithInt:checkin.checkInId]];
if (imageCheckin) {
cell.checkinImage.image = imageCheckin;
} else {
CompleteBlock block = ^(NSData* imageData) {
if (imageData) {
UIImage *image = [UIImage imageWithData:imageData];
[self.checkinImages setObject:image forKey:[NSNumber numberWithInt:checkin.checkInId]];
CheckinUITableViewCell *lookedUpCell = (CheckinUITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
if (lookedUpCell){
cell.checkinImage.image = image;
//[lookedUpCell setNeedsLayout];
}
}
};
[Utility loadImageFromFile:checkin.photoPath url:checkin.photoUrl withCompletionBlock:block];
}
return cell;
} else {
CheckinTextUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TextCellIdentifier];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CheckinTextUITableViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[CheckinTextUITableViewCell class]]) {
cell = (CheckinTextUITableViewCell *)currentObject;
if (self.userImage)
cell.userImage.image = self.userImage;
break;
}
}
}
cell.checkin = checkin;
return cell;
}
}
Any idea? Thanks!
The possible problem of your code is that your Cells have never been reused. NSBundle loadNibNamed: create new Cell each time your table need it. So if dataSource contain 10000 records - you creates 10000 cells.
Please check if you set cell identifier within xib file. This will cause to dequeue your cells.