I am using UISearchDisplayController to search and would like to add so more elements to each cell if that is possible. Did some searching but couldn't find any information. The only way is to reproduce it with tableview. Is there any other properties i can set other then textlabel.text
Thanks in advance!
You can change the cell completely.
The following snippet relies on two subclasses of UITableCell, ANormalCell & ASearchCell.
Caveat: This code has not been compiled but you get the gist?
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *normalCellReuseIdentifier = #"ANormalCell";
static NSString *searchCellReuseIdentifier = #"ASearchCell";
UITableViewCell* cell = nil;
if (tableView != self.searchDisplayController.searchResultsTableView) {
cell = (ANormalCell*)[self.tableView dequeueReusableCellWithIdentifier:normalCellReuseIdentifier];
if (cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ANormalCell" owner:nil options:nil];
cell = (ANormalCell*)[topLevelObjects objectAtIndex:0];
}
} else {
cell = (ASearchCell*)[self.tableView dequeueReusableCellWithIdentifier:searchCellReuseIdentifier];
if (cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ASearchCell" owner:nil options:nil];
cell = (ASearchCell*)[topLevelObjects objectAtIndex:0];
}
}
return cell;
}
Very possible. All of the tableView datasource methods are called with a tableView parameter. If
if (tableView == searchController.searchResultsTableView)
then alter the cell by adding subviews, or customizing properties. Same approaches apply for custom cells in this case as in any other.
Yes you can set cell.imageView , cell.accessoryView , cell.BackGroundView .. etc.
read this UITableViewCell documentation for further information
Related
well I have 2 plist files:
And:
My StoryBoard is:
the problem is when in my "Inicio" viewcontroller load data from the plist, I want Disable the cells when have the ENABLE=NO property & Enable the cells with ENABLE = YES, when I push or click in the cell, then go to the next view controller "PERSONAS" in this view controller load the 2nd Plist and in the same case I want go to the "DETALLE"viewcontroller only with the enabled cells that have ENABLED YES from plist.
for each viewdidload Im use:
NSString *myListPath = [[NSBundle mainBundle] pathForResource:#"MainMenuList" ofType:#"plist"];
mainMenu = [[NSArray alloc]initWithContentsOfFile:myListPath];
NSLog(#"%#",mainMenu);
NSString *myListPath = [[NSBundle mainBundle] pathForResource:#"PersonasList" ofType:#"plist"];
Personas = [[NSArray alloc]initWithContentsOfFile:myListPath];
NSLog(#"%#",Personas);
And for show the tableview in the custom cell Im use:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"CustomCell";
NSLog(#"CARGANDO CELDAS");
MainMenuCell *cell = (MainMenuCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCelliPhone" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.Enabled.text = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"ENABLE"];
cell.TitleLabel.text = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"NAME"];
cell.ImageImageView.image = [UIImage imageNamed:[[mainMenu objectAtIndex:indexPath.row]objectForKey:#"IMAGE"]];
if ([cell.Enabled.text isEqualToString:#"NO"])
{
NSLog(#"%#",cell.Enabled.text);
cell.userInteractionEnabled = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.selected = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
in the Persona view controller I use the same code but not working, all customs cells are enabled, how can I fix that?, or am I wrong in something? or I did Forget Something please help guys!!
in the Simulator run in that way for ENABLE:
But I dont wanna the cells with ENABLE NO run!!:
Please help guys, im using XCODE 5 DP6, for iOS7, how is the best way for solve this!!!
THANKS!!!!
for many intents, finally fix this!! well in each viewcontroller the code whit solve this is like:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"MainMenuCell";
NSLog(#"CARGANDO CELDAS");
MainMenuCell *cell = (MainMenuCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MainMenuCelliPhone" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
//MainMenuCell*cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
cell.Enabled.text = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Enabled"];
cell.TitleLabel.text = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Title"];
cell.ImageImageView.image = [UIImage imageNamed:[[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Image"]];
//cell.ImageImageView.image = [[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Image"];
if ([[[mainMenu objectAtIndex:indexPath.row]objectForKey:#"Enabled"] isEqualToString:#"NO"])
{
NSLog(#"%#",cell.Enabled.text);
cell.userInteractionEnabled = NO;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.selected = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.ImageImageView.image = [UIImage imageNamed:#"NO.png"];
}
else
{
cell.ImageImageView.image = [UIImage imageNamed:#"YES.png"];
}
return cell;
//------------------------------------------//
// THANKS anyway //
// GRETTINGS FROM BOLIVIA //
// ROCK ON!!!! n_n' //
//------------------------------------------//
}
I'll try to implement a TableView with 2 different kinds of cells, the first one is a higher Cell with a fullwidth image inside, all other cells got a little thumb image on the left side.
My problem it the performance of the TableView. If I scroll a list of maybe 20 items it jerks a bit. I've red something the performance and I'll hope the code is not so bad:
"Caching of images is not necessary to raise the performance" Is this right?
Does I reuse the cells in the right way.
Is that the normal way to use 2 different kinds of Cells?
Here is the important Part:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 && indexPath.row == 0) {
return 160;
}
return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellName = [[NSString alloc] init];
// [...]
if(indexPath.section == 0 && indexPath.row == 0){
cellName = #"FirstMainCell";
CellTemplateFirstNews *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if(cell == nil){
cell = [[[NSBundle mainBundle] loadNibNamed:#"CellTemplateFirstNewsView" owner:nil options:nil] objectAtIndex:0];
}
NSURL *urlRowImage = [NSURL URLWithString:[[NSString alloc]initWithFormat:#"http://webserver.de/inhalte/news/title/%#", detailDataNews.title_picture]];
NSData *dataRowImage = [NSData dataWithContentsOfURL:urlRowImage];
UIImageView *firstNewsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 160)];
firstNewsImageView.backgroundColor = [UIColor clearColor];
firstNewsImageView.opaque = NO;
firstNewsImageView.image = [UIImage imageWithData:dataRowImage];
cell.backgroundView = firstNewsImageView;
// [...]
return cell;
}else{
cellName = #"MainCell";
CellTemplateNews *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
if(cell == nil){
cell = [[[NSBundle mainBundle] loadNibNamed:#"CellTemplateNewsView" owner:nil options:nil] objectAtIndex:0];
}
NSURL *urlRowImage = [NSURL URLWithString:[[NSString alloc]initWithFormat:#"http://webserver.de/inhalte/news/cover/%#", detailDataNews.cover_picture]];
NSData *dataRowImage = [NSData dataWithContentsOfURL:urlRowImage];
UIImage *rowImage = [UIImage imageWithData:dataRowImage];
cell.thumbImage.image = rowImage;
// [...]
return cell;
}
}r
I think the main problem is the synchronous network request that's happening for every cell, whether it's reused or not. The culprit is dataWithContentsOfURL:.
Check out this apple sample code, or google the phrase "ios lazy table images". In this SO answer, I provide a fairly simple to implement method that takes care of the loading an image asynch, then finding and updating the correct table view row upon completion.
One posibility is that you've forgotten to enter the CellIdentifier in your Xib. In fact the reuseIdentifier method needs your cell to have the same identifier, but you never set it when you create your cell. Those cells in your Xib have to have their identifier entered.
Do not alloc-init any object in cellForRowAtIndexPath You are probably leaking massive memory. Remember, this method called every time a cell comes into view, even cells that have been previously shown.
Also, your assignments to string variables are redundant. It is sufficient to do it like this.
BOOL firstCell = (indexPath.row==0 && indexPath.section==0);
NSString *cellIdentifier = firstCell ? #"FirstMainCell" : #"MainCell";
UITableViewCell *genericCell = [tableView
dequeueReusableCellWithIdentifier:cellIdentifier];
if (firstCell) {
cell = (CellTemplateFirstNews*) genericCell;
// configure...
}
else {
cell = (CellTemplateNews*) genericCell;
// configure...
}
return cell;
The other possible reason for your lag is the synchronous call to the web service. You are doing this each time the cell becomes visible. You need to load the images lazily and update the UI when necessary.
I have a table view with several table view cell. The table view cell consists of title (using UILabel), picture (using UIWebView) and summary (using UITextView).
When I use UIImage for the picture, everything runs well. But then, I decided to display the pic by using resized UIWebView instead of UIImage. Some of the summary in the UITextView change unexpectedly.
When I scrolled the table, the problem will sometimes disappear. But it can randomly appear again on different table view cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
TableViewCell *cell = (TableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil];
cell = table;
self.table = nil;
}
//Configure the cell...
Article *article = [_articleRepository fetchArticleById:[articleIds objectAtIndex:indexPath.row]];
cell.headline.text = article.articleTitle;
// Remove html tag
NSString *substringContent = [self stringByStrippingHTML:article.articleContent];
cell.content.text = substringContent;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
ImageRepository *imageRepository = [[ImageRepository alloc]init];
Image *image = [imageRepository fetchImageById:article.articleImage];
NSMutableString *mutUrl = [[NSMutableString alloc]initWithString:image.imageUrl];
[mutUrl insertString:#"_thumb" atIndex:[mutUrl length]-4];
NSString *stringUrl = [[NSString alloc]initWithString:mutUrl];
NSString *htmlImage = [NSString stringWithFormat:#"<html><head><style type=\"text/css\">body{margin: 0; padding: 0;}</style></head><body><img src=\"%#\"></body></html>", stringUrl];
[cell.image loadHTMLString:htmlImage baseURL:nil];
return cell;
}
It could be due to reuse of cells in the tableview that make ur problem appear randomly across cells. Would help if you posted code of what you've done.
EDIT: Try changing ur portion of the code to the one given below.
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"TableViewCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
Some questions:
1. Why are you doing cell = table & self.table = nil ?
I have a view controller that shows up a table view controller when a button is clicked. Everything works fine with table view delegates, table view is shown fine, but in ellForRowAtIndexPath: delegate method, cell is instantiated and returned, but it's not shown correctly.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"alrededor";
alrededorCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];
NSLog(#"categoria %#", categoria);
cell.title.text = [categoria valueForKey:#"title"];
return cell;
}
Many thanks
Why are you creating your cell like this?
if (cell == nil) {
cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
If it is your own custom cell why do you use UITableViewCellStyleDefault ?
If the cell you are loading is a subclass of UITableViewCell and you've used the interface builder to build the cell you have to do a couple of things.
In the nib file delete the view that is there when you create it, add a UITableViewCell and change it's class to alrededorCell. Changes the cells class and not the file's owner. When you are linking buttons,labels etc . be sure to link them to the cell not to file's owner. Also set the cells uniqueIdentifier to alrededor.
In cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"alrededor";
alrededorCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *xib = [[NSBundle mainBundle] loadNibNamed:#"nibName" owner:nil options:nil];
for (alrededorCell *view in xib) {
if ([view isKindOfClass:[alrededorCell class]]) {
cell = view;
}
}
}
NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];
NSLog(#"categoria %#", categoria);
cell.title.text = [categoria valueForKey:#"title"];
return cell;
}
I try to fill my UITableView withd different UITableViewCells. I tried to choose 1 of 3 UITableViewCells out of a XIB-file by identifier but I can't get the value of the UITableViewCell's Identifier.
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MyTableViewCell *cell = (MyTableViewCell *) [tableView dequeueReusableCellWithIdentifier:#"CellTwo"];
if(nil == cell){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"MyTableViewCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[MyTableViewCell class]]) {
MyTableViewCell *tmpCell = (MyTableViewCell *) currentObject;
if (tmpCell.identifier == #"CellTwo") {
cell = (MyTableViewCell *) currentObject;
}
[tmpCell release];
break;
}
}
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;}
I solved this exact problem by creating a subclass for each UITableViewCell. When you do your comparison of the class type, you can put in the type of cell it is:
cell = (FooterTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"footerTableViewCell"];
if (cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CommonTableViewCell" owner:nil options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[FooterTableViewCell class]]){
cell = (FooterTableViewCell *)currentObject;
return cell;
}
}
}
I have some tables that reuse the exact same kind of cell formatting and i wanted to have them in a common XIB that i can alter at anytime. In the .h for that controller, i simply declared three sub classes of UITableViewCell for each of the three cell types.