How to stop reusing uitableviewCell? - ios

Hi in my app i have to load images to UITableViewCell .which are coming from the server.so the problem is when ever i scroll the tableview images are loading every time and its loading lazy so i tried to stop reusing cells but its not working ,How to stop reusing the cells in UITableView.
my code is
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"Cell";
RestaurantCustomCell *cell =(RestaurantCustomCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *topLevelObjects ;
topLevelObjects= [[NSArray alloc]init];
if(cell==nil)
{
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RestaurantCustomCell" owner:self options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[UITableViewCell class]])
{
cell = (RestaurantCustomCell *) currentObject;
break;
}
}
}
NSDictionary *dict=[restauarantsArr objectAtIndex:indexPath.row];
NSString *imgStr=[dict valueForKey:#"Img"];
[self processImageDataWithURLString:imgStr andBlock:^(NSData *imageData) {
if (self.view.window)
{
UIImage *img = [UIImage imageWithData:imageData];
if(img!=nil)
{
UIGraphicsBeginImageContext(CGSizeMake(100, 100));
[img drawInRect:CGRectMake(0,0,100,100)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
cell.restaurantImg.image=newImage;
UIGraphicsEndImageContext();
}
}
}];
cell.nameLbl.text=[dict valueForKey:#"Restaurants"];
cell.typeLbl.text=[dict valueForKey:#"Rest_Cuisine"];
cell.timingsLbl.text=[dict valueForKey:#"Rest_Timings"];
cell.categoryLbl.text=[dict valueForKey:#"Rest_Category"];
cell.addressLbl.text=[dict valueForKey:#"Address"];
return cell;
}

You can not doing this in default table view cell. If you want to create this kind of things then you need to create custom tableview cell anbd do it what you want.

You have to make a custom cell like that:
RestaurantCustomCell.h
#import <UIKit/UIKit.h>
#interface RestaurantCustomCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *nameLbl;
#property (weak, nonatomic) IBOutlet UILabel *typeLbl;
#property (weak, nonatomic) IBOutlet UILabel *timingsLbl;
#property (weak, nonatomic) IBOutlet UILabel *categoryLbl;
#property (weak, nonatomic) IBOutlet UILabel *addressLbl;
#property (weak, nonatomic) IBOutlet UIImageView *restauranImg;
#end
RestaurantCustomCell.m
#import "RestaurantCustomCell.h"
#implementation RestaurantCustomCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)prepareForReuse{
self.nameLbl.text = #"";
self.typeLbl.text = #"";
self.timingsLbl.text = #"";
self.categoryLbl.text = #"";
self.addressLbl.text = #"";
}
#end
after in IB you add a new UITableViewCell at your UITableView and the class of it with you new custom cell set the Identify ID like "RestaurantCustomCell" add your labels and imageView to your custom cell and connect the Outlet, then you modify you tableView:cellForRowAtIndexPath: like that:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"RestaurantCustomCell";
RestaurantCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *dict=[restauarantsArr objectAtIndex:indexPath.row];
NSString *imgStr=[dict valueForKey:#"Img"];
[self processImageDataWithURLString:imgStr andBlock:^(NSData *imageData) {
if (self.view.window)
{
UIImage *img = [UIImage imageWithData:imageData];
if(img!=nil)
{
UIGraphicsBeginImageContext(CGSizeMake(100, 100));
[img drawInRect:CGRectMake(0,0,100,100)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
cell.restaurantImg.image=newImage;
UIGraphicsEndImageContext();
}
}
}];
cell.nameLbl.text=[dict valueForKey:#"Restaurants"];
cell.typeLbl.text=[dict valueForKey:#"Rest_Cuisine"];
cell.timingsLbl.text=[dict valueForKey:#"Rest_Timings"];
cell.categoryLbl.text=[dict valueForKey:#"Rest_Category"];
cell.addressLbl.text=[dict valueForKey:#"Address"];
return cell;

Related

Label in UITableViewCell is not getting accessed in .m file

I have a tableview in my view controller. I took one protype cell in which I created a label and connected it to the tableVieCell.h file. The problem is when I try to access the label in my view to get it loaded with the textfield input, I am not getting the label component. Please check. Here is my MyCell.h file.
#import <UIKit/UIKit.h>
#interface MyCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *label1;
#property (strong, nonatomic) IBOutlet UILabel *label2;
#property (strong, nonatomic) IBOutlet UITextField *celltextField;
#end
here is my viewController.h file.
#import <UIKit/UIKit.h>
#import "MyCell.h"
#interface ViewController :UIViewController<UITableViewDelegate,UITableViewDataSource>
{
NSMutableArray *contacts;
}
- (IBAction)leftBtn:(id)sender;
- (IBAction)rightBtn:(id)sender;
- (IBAction)nextBtn:(id)sender;
- (IBAction)SendBtn:(id)sender;
#property (strong, nonatomic) IBOutlet UITableView *tabV1;
#property (strong, nonatomic) IBOutlet UITextField *txtField;
#end
#import "ViewController.h"
#interface ViewController ()
{
MyCell *cell;
}
#end
#implementation ViewController
- (void)viewDidLoad {
contacts= [[NSMutableArray alloc]init];
[super viewDidLoad];
}
-(BOOL)textFieldShouldReturn:(UITextField *) textField{
[textField resignFirstResponder];
return YES;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return contacts.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// [cell.zoomButton addTarget:self action:#selector(navigateAction:) forControlEvents:UIControlEventTouchUpInside];
// cell.zoomButton.tag=indexPath.row;
cell= [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
// cell.textLabel.text=contacts[indexPath.row];
cell.label2.text=[contacts objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)leftBtn:(id)sender {
}
- (IBAction)rightBtn:(id)sender {
}
- (IBAction)nextBtn:(id)sender {
}
-(void)someMethod:(UIButton *)sender{
}
- (IBAction)SendBtn:(id)sender {
// if (_leftBtn.tag==1) {
// cell.label1.text=_txtField.text;
//
// }else{
// cell.label2.text=_txtField.text;
// }
[self->contacts addObject:_txtField.text];
// label2.text=_txtField.text;
[_tabV1 reloadData];
_txtField.text=#"";
}
#end
Change this
cell= [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
to this
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];

Why won't this UIButton image show in a custom UITableViewCell?

I'm trying to add a "favorite button" to a custom cell in a UIableView.
The cell has a label and detailLabel that show fine. I must be doing something wrong but I can't figure out what it is. My code is below, and I changed my original method (commented) after studying this question.
My code is as follows:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyCustomCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
}
cell.nameLabel.text = [myMutableArray valueForKey:#"Name"];
cell.detailLabel.text = [myMutableArray valueForKey:#"Detail"];
if (isFavorite)
{
NSLog(#"fave");
UIImage *btnImage = [UIImage imageNamed:#"goldStar.png"];
[cell.favoriteButton setImage:btnImage forState:UIControlStateNormal];
//[cell.favoriteButton setImage:[UIImage imageNamed:#"yellowStar.png"] forState:UIControlStateNormal];
} else {
NSLog(#"out of fave");
UIImage *btnImage = [UIImage imageNamed:#"greyStar.png"];
[cell.favoriteButton setImage:btnImage forState:UIControlStateNormal];
//[cell.favoriteButton setImage:[UIImage imageNamed:#"greyStar.png"] forState:UIControlStateNormal];
}
return cell;
}
MyCustomCell.h
#interface MyCustomCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel *nameLabel;
#property (nonatomic, weak) IBOutlet UILabel *detailLabel;
#property (nonatomic, strong) IBOutlet UIButton *favoriteButton;
MyCustomCell.m
#implementation MyCustomCell
#synthesize nameLabel = _nameLabel;
#synthesize detailLabel = _detailLabel;
#synthesize favoriteButton = _favoriteButton;
- (void)awakeFromNib {
_favoriteButton = [[DBTileButton alloc] init];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
}
I've checked and the button is hooked up ok in IB. Thanks ahead for any advice.
UPDATE I notice in the log message that the image frame is set at 0,0,0,0. I'm attaching a shot of the custom cell IB setup. Is something amiss there?
As far as I know IBOutlet for any sub classes of UIView shouldn't be strong! Anyway you no need to use IBOutlet, if you wanna initialize your button programmatically: at first, you forgot to set frame of your button and add it to cell view via addSubview. But it is easier way: just add button on storyboard, set custom class and remove this:
_favoriteButton = [[DBTileButton alloc] init];
EDIT: set class for your storyboard's button like on picture
Then change your outlet:
#property (nonatomic, weak) IBOutlet DBTileButton *favoriteButton;
And finally don't forget to connect outlet to your button and remove programmatically initializing.

UITableViewCell display image but not text

I have a custom UITableViewCell which contains an UIImageView and two UILabel. When, I run the app, UIImageVIew get displayed but not UILabel.
Here is my Code:
ViewController.m
-(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myTableView.dataSource = self;
self.myTableView.delegate = self;
titleLabel = #[#"Kiruba",#"Kiruba",#"Kiruba",#"Kiruba"];
subtitleLabel = #[#"xxx",#"xxx",#"CHN",#"CHN"];
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return titleLabel.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
// NSLog(#"Text : %#",[titleLabel objectAtIndex:indexPath.row]);
cell.TitleLabel.text = [titleLabel objectAtIndex:indexPath.row];
cell.SubtitleLabel.text = [subtitleLabel objectAtIndex:indexPath.row];
cell.myImageView.image = [UIImage imageNamed:#"DSCN5478.JPG"]
return cell;
}
CustomCell.h :
#interface CustomCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *TitleLabel;
#property (weak, nonatomic) IBOutlet UILabel *SubtitleLabel;
#property (weak, nonatomic) IBOutlet UIImageView *myImageView;
Any idea, Why the UIImageView get displayed but not UILabel?
I'm using Xcode 5 and set target IOS version as 7.
Add your UILabel and UIImageView to the contentView of your custom UITableViewCell.

Set label for a custom cell

I have a custom table cell and I'm just trying to set the label, image, etc but for some reason it's not working.
Here is my code for my custom cell
BrowserMenuCell.h
#import <UIKit/UIKit.h>
#interface BrowseMenuCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UIButton *wishListBUtton;
#property (strong, nonatomic) IBOutlet UIImageView *itemImage;
#property (strong, nonatomic) IBOutlet UILabel *itemLabel;
#property (strong, nonatomic) IBOutlet UILabel *itemPrice;
#property (strong, nonatomic) IBOutlet UITextField *quantityField;
#property (strong, nonatomic) IBOutlet UILabel *totalLabel;
- (IBAction)plusButton:(id)sender;
- (IBAction)cartButton:(id)sender;
- (IBAction)minusButton:(id)sender;
#end
BrowserMenuCell.m
#import "BrowseMenuCell.h"
#implementation BrowseMenuCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (IBAction)plusButton:(id)sender {
}
- (IBAction)cartButton:(id)sender {
}
- (IBAction)minusButton:(id)sender {
}
#end
Cell for row at index path
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BrowseMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ItemsCell"];
OfficeSupply *supply = [_items objectAtIndex:indexPath.row];
if(cell == nil)
{
NSArray * views = [[NSBundle mainBundle] loadNibNamed:#"BrowseMenuCell" owner:nil options:nil];
for (id obj in views){
if([obj isKindOfClass:[UITableViewCell class]])
{
BrowseMenuCell * obj = [[BrowseMenuCell alloc]init];
obj.itemLabel.text = supply.itemName;
cell = obj;
break;
}
}
}
return cell;
}
Have you tried using [self.tableView reloadData]?
To start with, don't create a new random instance of the cell after you have already created one from the NIB file. Change the code to this:
for (BrowseMenuCell *obj in views){
if([obj isKindOfClass:[BrowseMenuCell class]])
{
obj.reuseIdentifier = #"ItemsCell";
obj.itemLabel.text = supply.itemName;
cell = obj;
break;
}
}
That won't guarantee it works, but at least the NIB setup of your labels won't be getting thrown away.
If it still doesn't work. Debug. Check the frames of the labels. Check the label references are set (not nil).
This is the wrong way you create cell. Try this one:
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BrowseMenuCell *cell = (BrowseMenuCell*)[tableView dequeueReusableCellWithIdentifier:#"ItemsCell"];
OfficeSupply *supply = [_items objectAtIndex:indexPath.row];
if (_cellObj == nil) {
[[NSBundle mainBundle] loadNibNamed:#"BrowseMenuCell" owner:nil options:nil];
}
cell.itemlabel.text = supply.itemName;
return cell;
}

collection view cell display not displaying

I have a collectionview controller and a collectionviewcell. FOr the collectionviewcell I have a custom class. I am trying to set the uiimage and it isn't working.
Thank you in advance!.
Method in collectionview controller where I am trying to set the image.
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier=#"Cell";
CollectionVIewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
;
CollectionVIewCell *feederCell = (CollectionVIewCell*)[_availableFilters objectAtIndex:indexPath.row];
cell.labelDisplay.text=feederCell.filterName;
NSLog(#"selected =%#",cell.labelDisplay.text);
if (feederCell.isSelected) {
UIImageView *imageView=[[UIImageView alloc]init];
//process string name
NSString*filePath =[NSString stringWithFormat:#"%#.png",feederCell.filterName];
NSArray* words = [filePath componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceCharacterSet]];
filePath=[words componentsJoinedByString:#""];
cell.FilePath=filePath;
//image
UIImage *image = [UIImage imageNamed:filePath];
[imageView setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
[imageView setImage:image];
[cell setIconDisplay:imageView];//setting the image that won't set
}else if(!feederCell.isSelected){
UIImageView *imageView=[[UIImageView alloc]init];
NSString*filePath =[NSString stringWithFormat:#"%#.png",feederCell.filterName];
NSArray* words = [filePath componentsSeparatedByCharactersInSet :[NSCharacterSet whitespaceCharacterSet]];
filePath=[words componentsJoinedByString:#""];
UIImage *image = [UIImage imageNamed:filePath];
[imageView setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
[imageView setImage:image];
[cell setIconDisplay:imageView];
}
return cell;
.h file
//
// CollectionVIewCell.h
#import <UIKit/UIKit.h>
#interface CollectionVIewCell : UICollectionViewCell
#property (strong, nonatomic) IBOutlet UIImageView *IconDisplay;
#property (strong, nonatomic) IBOutlet UILabel *labelDisplay;
#property (assign,nonatomic) BOOL isSelected;
#property (strong,nonatomic)NSString*textName;
#property(strong,nonatomic)NSString*FilePath;
#end
.m file
//
// CollectionVIewCell.m
#import "CollectionVIewCell.h"
#implementation CollectionVIewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_labelDisplay.text=_textName;
_IconDisplay=[[UIImageView alloc]init];
}
return self;
}
#end
You need to add the UIImageView as a subview. Otherwise it exists but isn't displayed.

Resources