at the Moment i have the following classes:
My Problem is that my Custom Cell Class "GTNewsCustomCell" is never called, i set some breakpoints in the .m file but nothing happend, than i realized that in my "cellForRowAtIndexPath" the cell is never nil!
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier1 = #"NewsCell";
GTNewsCustomCell *newsCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if(newsCell == nil){
newsCell = [[GTNewsCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1];
}
newsCell.titleLabel.text = [[self.newsList objectAtIndex:indexPath.section]objectForKey:#"title"];
NSAttributedString *attString = [[NSAttributedString alloc]initWithString:[[self.newsList objectAtIndex:indexPath.section]objectForKey:#"previewMessage"]];
newsCell.messageTextView.attributedText = attString;
return newsCell;
}
And here is a little part of my Code from my GTNewsCustomCell.m:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.messageTextView.textColor = [UIColor blackColor];
self.messageTextView.backgroundColor = GTDefaultTextBackgroundColor;
self.messageTextView.editable = NO;
self.messageTextView.userInteractionEnabled = NO;
self.messageTextView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSString *htmlTag = #"<b></b>";
NSString *html = [NSString stringWithFormat:#"%#%#",htmlTag,self.messageTextView.attributedText];
NSData *data = [html dataUsingEncoding:NSUTF8StringEncoding];
CGFloat fontSizeMultiplier = 1.1;
CGFloat const DTCoreTextDefaultFontSize = 12.0;
NSString * fontName = #"Helvetica";
DTCSSStylesheet* css;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"7.0")){
css = [[DTCSSStylesheet alloc] initWithStyleBlock:#"ul li{padding-left: -10px;}"];
NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:fontSizeMultiplier], NSTextSizeMultiplierDocumentOption,
fontName, DTDefaultFontFamily,
#"purple", DTDefaultLinkColor,
#"red", DTDefaultLinkHighlightColor,
css,DTDefaultStyleSheet,
[NSNumber numberWithBool:YES],DTUseiOS6Attributes,
nil];
.
.
.
.
Here are some screenshots from my xib Files for more information:
My GTNewsTableViewController:
GTNewsCustomCell (FilesOwner):
GTNewsCustomCell (View):
GTNewsCustomCell:
Okay, since you're doing the registration of cells via tableView's method registerNib:forCellReuseIdentifier:. The initWithStyle:reuseIdentifier: won't ever get called. If you'd like to do the styling, use -awakeFromNib method of your UITableViewCell subclass.
- (void)awakeFromNib {
[super awakeFromNib];
self.messageTextView.textColor = [UIColor blackColor];
self.messageTextView.backgroundColor = GTDefaultTextBackgroundColor;
self.messageTextView.editable = NO;
self.messageTextView.userInteractionEnabled = NO;
}
with GTNewsCustomCell (FilesOwner), you will not be able to reuse this cell if needed. you should make it NSObject(FileOwner). and load this cell in cellForRowAtIndexPath.
by this way , you will get the same style of of your custom cell.
static NSString *CellIdentifier= #"CustomCell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:#"CustomCell"
owner:self//transfer ownership to self
options:nil];
cell = [nibViews objectAtIndex:0];
}
return cell;
}
Related
What I'm trying to do is load some HTML resource files in to UITableView cells. I wanted to adjust the cell heights depend on the content size of the UIWebView. Following is some of my test codes.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"TableCellID";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:#"CustomTableViewCell" owner:self options:nil];
cell = [nibArray objectAtIndex:0];
}
NSString *htmlFileName = [NSString stringWithFormat:#"page%ld", (long)indexPath.row];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:htmlFileName ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[cell.webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]];
return cell;
}
If I describe my CustomTableViewCell.xib, it has a UIWebView inside the Content View and UIWebView has following constraints given.
ContentView.Bottom = WebView.Bottom + 8
ContentView.Top = WebView.Top +8
ContentView.Leading = WebView.Leading + 8
ContentView.Trailing = WebView.Trailing + 8
HTML fils has no JavaScript or CSS content. Simple as below.
<html>
<header>
</header>
<body>
<p>eeeee eeeee eeeee eeeee eeeee eeeee eeeee eeeee eeeee eeeee</p>
</body>
</html>
Now my out put is like below image.
As I explained at top, my web pages doesn't contain any JavaScript. So I may not able to use stringByEvaluatingJavaScriptFromString method to return the text.
Try this
- (void)viewDidLoad {
[super viewDidLoad];
arrOfHTMLList= [[NSMutableArray alloc] init];
NSMutableDictionary *dic1 = [[NSMutableDictionary alloc] init];
[dic1 setObject:#"0" forKey:#"height"];
[dic1 setObject:#"aaaaaaaaaa aaaaaa aaaaaaa aaaaaaa aaaaaa aaaaaa aaaaaaaa aaaaa" forKey:#"htmlString"];
NSMutableDictionary *dic2 = [[NSMutableDictionary alloc] init];
[dic1 setObject:#"0" forKey:#"height"];
[dic1 setObject:#"abababababbabababababab ababab bababa ababababb" forKey:#"htmlString"];
NSMutableDictionary *dic3 = [[NSMutableDictionary alloc] init];
[dic1 setObject:#"0" forKey:#"height"];
[dic1 setObject:#"sdajdkjas dkasjkhd jkashjkd sakjhdkj askjdaksh dksajkdkjsah djkasjkhd kjsahjkd asjkhdk jasjkdhaskj" forKey:#"htmlString"];
NSMutableDictionary *dic4 = [[NSMutableDictionary alloc] init];
[dic1 setObject:#"0" forKey:#"height"];
[dic1 setObject:#"sdajdkjas dkasjkhd jkashjkd sakjhdkj askjdaksh dksajkdkjsah djkasjkhd kjsahjkd asjkhdk jasjkdhaskj" forKey:#"htmlString"];
[arrOfHTMLList addObject:dic1];
[arrOfHTMLList addObject:dic2];
[arrOfHTMLList addObject:dic3];
[arrOfHTMLList addObject:dic4];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
UIWebView *webView = (UIWebView*)[cell viewWithTag:1];
NSString *myHTML = arrOfHTMLList[indexPath.section][#"htmlString"];
[webView loadHTMLString:myHTML baseURL:nil];
webView.delegate = self;
webView.frame = CGRectMake(0, 0, screenWidth,cell.frame.size.height);
return cell;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
for (UIView *parent=[webView superview];parent!=nil ;parent=[parent superview] )
{
if([parent isKindOfClass:[UITableViewCell class]])
{
UITableViewCell *cell=(UITableViewCell *)parent;
NSIndexPath *indexPath=[_tableView indexPathForCell:cell];
int height = webView.scrollView.contentSize.height;
[[arrOfHTMLList objectAtIndex:indexPath.section] setObject:[NSString stringWithFormat:#"%d",height] forKey:#"height"];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = [arrOfHTMLList[indexPath.section][#"height"] intValue];
return height;
}
Hope it helps.
the main issue is to get a web view's frame to adapt to its html content right away :)
and THAT only happens after it asynchronously loaded the content!
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
//TODO: update the table's height for the cell for the webview
}
a drop-in solution I wrote is a custom UITableViewCell subclass that has a webview and informs a delegate to reload the table:
https://github.com/Daij-Djan/DDUtils/tree/master/objC/DDUtils-IOS/ui/M42WebviewTableViewCell%20%5Bios%5D
the most important piece of code is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
M42WebviewTableViewCell *cell = _loadedCells[#(indexPath.row)];
if (!cell) {
cell = [[M42WebviewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
NSString *object = _objects[indexPath.row];
cell.tag = indexPath.row;
cell.delegate = self;
[cell setHtml:object AndBaseURL:nil];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//if a cell isnt ready, it wont be there and the method will return 0
M42WebviewTableViewCell *cell = _loadedCells[#(indexPath.row)];
return cell.height;
}
#pragma mark - M42WebviewTableViewCellDelegate
- (void)tableCellDidLoadContent:(M42WebviewTableViewCell *)cell {
if (!_loadedCells) {
_loadedCells = [[NSMutableDictionary alloc] init];
}
[_loadedCells setObject:cell forKey:#(cell.tag)];
//only refresh sizes of rows
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
I came up with my own solution. But thanks all the others who commented on this question and open my mind in different paths. So here what I did.
First added a UIWebViewDelegate to my ViewController class
#interface ViewController () <UITableViewDataSource, UITableViewDelegate, UIWebViewDelegate> {
Then loading HTML pages and adding delegate is done inside following method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"TableCellID";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:#"CustomTableViewCell" owner:self options:nil];
cell = [nibArray objectAtIndex:0];
}
cell.webView.delegate = self;
cell.webView.tag = indexPath.row;
cell.webView.scrollView.scrollEnabled = NO;
NSString *htmlFileName = [NSString stringWithFormat:#"page%ld", (long)indexPath.row];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:htmlFileName ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[cell.webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]];
return cell;
}
After web view is loaded, calculate the size that fits to it's content and after all web views are loaded, I reload table data like below.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (webView.tag == 0) {
[cellHeightMutArray removeAllObjects];
}
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
[cellHeightMutArray insertObject:#(fittingSize.height) atIndex:webView.tag];
if (((webView.tag + 1) == dataArray.count) && !reloadedTableViewAfterLoadingWebContent) {
reloadedTableViewAfterLoadingWebContent = YES;
_tableView.hidden = NO;
[_tableView reloadData];
}
}
Then I adjust the height of the cell (which was the main issue that I had)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (cellHeightMutArray.count > 0) {
return [[cellHeightMutArray objectAtIndex:indexPath.row] intValue];
}
return 44;
}
This is my solution and it works perfectly in iPhones and iPads.
But I had different issue when I run it with more complex user interface in iPads which is in my real project. I'll update this if my solutions for those issue related to this question.
I've got a Table View Cell in .xib. I've got a UILabel and UIImageView in the .xib.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
SchoolsTableViewCell *cell = (SchoolsTableViewCell *) [[[NSBundle mainBundle] loadNibNamed:#"SchoolsTableViewCell" owner:self options:nil] lastObject];
NSLog(#"Before: %#", cell.nameLabel.text);
cell.nameLabel.text = [NSString stringWithFormat:#"Name: %#", [object objectForKey:#"name"]];
NSLog(#"After: %#", cell.nameLabel.text);
PFFile *file = [object objectForKey:#"logo"];
[file getDataInBackgroundWithBlock:^(NSData *data, NSError *error)
{
cell.logoImageView.image = [UIImage imageWithData:data];
}];
return cell;
}
As you can see, I have 2 NSLog()s, printing the below:
2015-07-08 20:17:26.739 o [1871:108786] Before: (null)
2015-07-08 20:17:26.740 o [1871:108786] After: Name: SMSAS
That is expected.
However, on my Simulator, I can only see a blank cell with Name:. It should show SMSAS in the label, and load a logo, but it's not happening. I can pass the object to the next View Controller when the cell is tapped, and it can show in that View Controller.
This is my .m for the cell.
#implementation SchoolsTableViewCell
- (void)awakeFromNib
{
[super awakeFromNib];
self.nameLabel = [[UILabel alloc] init];
self.logoImageView = [[UIImageView alloc] init];
[self.contentView addSubview:self.nameLabel];
[self.contentView addSubview:self.logoImageView];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
}
#end
I'm sure it's a simple mistake. Anyone care to open my eyes? Thanks!
Use dequeueReusableCellWithIdentifier
static NSString * CellIdentifier = #"SchoolsTableViewCell";
SchoolsTableViewCell *cell = (SchoolsTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"SchoolsTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
I am parsing JSON and storing it in an array.I have a custom cell subclass in which I am trying to set the title of a label from the content of parsed JSON.
This is the parsing process.
#implementation BlogScreenViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.jsonArray = [[NSMutableArray alloc]init];
NSError *error = nil;
NSURL *url = [NSURL URLWithString:#"http:web_services/blog.php"];
NSData *data = [NSData dataWithContentsOfURL:url];
self.jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
for (NSDictionary *obj in self.jsonArray)
{
NSLog(#"JSON string output :- %# ", [obj objectForKey:#"titre"]); // shows titles
}
In custom cell subclass
// set blog title label
UILabel *blogTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 20, 200, 20)];
// blogTitleLabel.text = #"This is blog title"; // This shows properly if used.
for (NSDictionary *obj in blogScreenVC.jsonArray ) //This doesn't work.
{
blogTitleLabel.text = [NSString stringWithFormat:#"%#",[obj objectForKey:#"titre"]];
}
[blogTitleLabel setFont:[UIFont systemFontOfSize:9]];
[self addSubview:blogTitleLabel];
Instead of Setting lebel.text in customcell class add it in the cellForRowAtIndexPath.
Create Property of label in customcell.h file
#property (nonatomic, weak) IBOutlet UILabel *label;
And in customcell.m file synthesys it.
#synthesize label = _label;
And in you viewcontroller.m file
#import "CustomCell.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
if (self.jsonArray.count !=0) {
NSMutableDictionary * tmpDictn = [self.jsonArray objectAtIndex:indexPath.row];
if ([tmpDictn objectForKey:#"titre"] != nil)
{
cell.label.text = [tmpDictn objectForKey:#"titre"];
}else{
cell.label.text = #"tmpDictn does not contain data";
}
}else{
cell.label.text = #"jsonArray does not contain data";
}
return cell;
}
im newbie IOS and using IB, im working UITableView in UIViewController, i have implement "UITableViewDelegate, UITableViewDataSource" in viewcontroller and setdelegate,datasource for uitableview but it dosen't work, i dont know,
please help me!
thank for your read this article.
code viewcontroller.h
#interface ViewController : UIViewController<UITableViewDelegate,
UITableViewDataSource> #property (nonatomic, retain) IBOutlet UITableView *tableView;
code viewcontroller.m
- (void)viewDidLoad { [tableView setDataSource:self];
[tableView setDelegate:self];
dispatch_async(htvque, ^{
NSData* data = [NSData dataWithContentsOfURL: listFilmByCate];
NSError* error;
jsonTable = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
listDataTable = [jsonTable objectForKey:#"List"];
dispatch_async(dispatch_get_main_queue(), ^{
[tableView reloadData];
});
});
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return listDataTable.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dataTable = [listDataTable objectAtIndex:indexPath.row];
NSData *receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[dataTable objectForKey:#"Thumbnail"]objectForKey:#"Url1"]]]; UIImage *image = [[[UIImage alloc] initWithData:receivedData] stretchableImageWithLeftCapWidth:50 topCapHeight:80];
static NSString *simple_cell = #"simpleCell";
CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];
if (simple_cell == nil)
{}
customize_cell.imageView.image = image;
customize_cell.lbldescription.text =[dataTable objectForKey:#"LongDescription"];
customize_cell.lblTitle.text = [dataTable objectForKey:#"VName"];
customize_cell.lblTitle.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.numberOfLines=4;
customize_cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bgtblRight.png"]];
return customize_cell;
}
in this line of code is your problem:
CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];
if (simple_cell == nil)
{}
what would happens if customize_cell is nil?
you are not instanciating the customize_cell and because of that you cant call these methods:
customize_cell.imageView.image = image;
customize_cell.lbldescription.text =[dataTable objectForKey:#"LongDescription"];
customize_cell.lblTitle.text = [dataTable objectForKey:#"VName"];
customize_cell.lblTitle.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.numberOfLines=4;
customize_cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bgtblRight.png"]];
return customize_cell;
just add a simple inicialize cell inside the if, like this:
if (customize_cell == nil)
{
customize_cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"AnIdentifierString"] autorelease];
}
EDIT
change this:
CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];
if (simple_cell == nil)
{}
customize_cell.lbldescription.text =[dataTable objectForKey:#"LongDescription"];
customize_cell.lblTitle.text = [dataTable objectForKey:#"VName"];
customize_cell.lblTitle.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.numberOfLines=4;
customize_cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bgtblRight.png"]];
return customize_cell;
to:
CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];
if (customize_cell == nil)
{
customize_cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"AnIdentifierString"] autorelease];
}
customize_cell.lbldescription.text =[dataTable objectForKey:#"LongDescription"];
customize_cell.lblTitle.text = [dataTable objectForKey:#"VName"];
customize_cell.lblTitle.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.lineBreakMode = UILineBreakModeWordWrap | UILineBreakModeTailTruncation;
customize_cell.lbldescription.numberOfLines=4;
customize_cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"bgtblRight.png"]];
return customize_cell;
Set,
tableView. dataSource = self;
tableView. delegate = self;
Implement protocol which are directed as #required means to say mandatory
-(UITableViewCell*) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath
{
// dint concentrate on this part, hope u are right
NSDictionary *dataTable = [listDataTable objectAtIndex:indexPath.row];
NSData *receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[dataTable objectForKey:#"Thumbnail"]objectForKey:#"Url1"]]];
UIImage *image = [[[UIImage alloc] initWithData:receivedData] stretchableImageWithLeftCapWidth:50 topCapHeight:80];
static NSString *simple_cell = #"simpleCell";
// works like double ended queue.... cells are re-used when available... so when cell is nil, u need to create
CustomizeCell_Home *customize_cell = (CustomizeCell_Home *)[tableView dequeueReusableCellWithIdentifier:simple_cell];
if (simple_cell == nil)
{
// CustomizeCell_Home is linked to CustomizeCell_Home_Reference using nib,
[[NSBundle mainBundle] loadNibNamed: #"CustomizeCell_Home" owner: self options: nil];
customize_cell = CustomizeCell_Home_Reference;
}
.....
.....
return customize_cell;
}
First thing, the cells are never allocated, instead of doing if(simple_cell==nil), you should do if(customize_cell==nil) and do the initialization code there. Otherwise you're comparing a string that you just created and set a value to against nil.
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];