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.
Related
I need to use two UITableView in one UITableViewController. And I need to create two custom cell for each UITableView.
Can I possible to make this? please help me.
try this.....used two UITableView in one UIViewController...
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView==self.tableview1)
{
return tableview1RowCount;
}
else if(tableView==self.tableview2)
{
return tableview2RowCount;
}
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView==self.tableview1)
{
static NSString *CellIdentifier1 = #"tableview1_cell";
tableview1_cell *cell1 = (tableview1_cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell1 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"tableview1_cell" owner:self options:nil];
cell1 = [nib objectAtIndex:0];
}
return cell1;
}
if (tableView==self.tableview2)
{
static NSString *CellIdentifier1 = #"tableview2_cell";
tableview2_cell *cell2 = (tableview2_cell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell2 == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"tableview2_cell" owner:self options:nil];
cell2 = [nib objectAtIndex:0];
}
return cell2;
}
}
and for the method
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
do same as above....separate tableviews with if condition and give actions to each...whatever u want...
I have a tableview, the tableview picks up data from a dictionary and shows it in the tableview. I also have a Custom tableview cell. The data which is displayed in the tableview, I need to show that in the custom tableview cell.
This is the code from my tableview:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"HistoryViewTableCell"];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"HistoryViewTableCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
NSDictionary * tempDictionary = [tableData objectAtIndex:indexPath.row];
cell.textLabel.text = [tempDictionary objectForKey:#"PickupAddress"];
return cell;
}
I need to show this data in my custom tableview cell. If it's not very clear, please ask and I will try to explain the best I can.
You can override 'new' method in order to load cell from xib. Try this one
+(instancetype) new
{
id cell = nil;
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[self class]])
{
cell = currentObject;
break;
}
}
NSParameterAssert(cell);
return cell;
}
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;
}
I have two tableviewcell in xib file and I use both of them.
When I use one of them the memory is still 7.3MB, but when I use both memory grow very fast.
Please help me. I don't now what is wrong.
My code is below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
NSDictionary *rowData = [self.events objectAtIndex:row];
static NSString *CellIdentifierL = #"LeftCell";
static NSString *CellIdentifierR = #"RightCell";
SampleTableCell *cellLeft = [tableView dequeueReusableCellWithIdentifier:CellIdentifierL];
SampleTableCell *cellRight = [tableView dequeueReusableCellWithIdentifier:CellIdentifierR];
if (cellLeft == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"LeftTableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SampleTableCell class]]) {
cellLeft = (SampleTableCell *)currentObject;
break;
}
}
}
if (cellRight == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RightTableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SampleTableCell class]]) {
cellRight = (SampleTableCell *)currentObject;
break;
}
}
}
return (row%2==0)?cellRight:cellLeft;
}
You need to create only the cell you need. Your code should be like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
NSDictionary *rowData = [self.events objectAtIndex:row];
static NSString *CellIdentifierL = #"LeftCell";
static NSString *CellIdentifierR = #"RightCell";
if (cellLeft == nil) {
SampleTableCell *cellLeft = [tableView dequeueReusableCellWithIdentifier:CellIdentifierL];
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"LeftTableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SampleTableCell class]]) {
cellLeft = (SampleTableCell *)currentObject;
break;
}
}
return cellLeft;
}
if (cellRight == nil) {
SampleTableCell *cellRight = [tableView dequeueReusableCellWithIdentifier:CellIdentifierR];
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RightTableCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SampleTableCell class]]) {
cellRight = (SampleTableCell *)currentObject;
break;
}
}
return cellRight;
}
return nil;
}
I change to this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
NSDictionary *rowData = [self.events objectAtIndex:row];
SampleTableCell *cell;
if(row%2==0){
cell = [[[NSBundle mainBundle] loadNibNamed:#"RightTableCell" owner:self options:nil]objectAtIndex:0];
}else{
cell = [[[NSBundle mainBundle] loadNibNamed:#"LeftTableCell" owner:self options:nil]objectAtIndex:0];
}
cell.labelName.text = [rowData objectForKey:#"name"];
return cell;
}
But I don't know that is good idea?
This is not a good way to do this. You are creating both cells for every row, but you only use one of them. You should also use the newer way to use a xib based cell -- that is, use registerNib:forCellWithReuseIdentifier: in viewDidLoad, to register both nibs. Do your check for odd/even rows first, and dequeue the correct cell (no need to check for nil when using the register method). You do it like this:
- (void)viewDidLoad {
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:#"SampleCell1" bundle:nil] forCellReuseIdentifier:#"SampleCell1"];
[self.tableView registerNib:[UINib nibWithNibName:#"SampleCell2" bundle:nil] forCellReuseIdentifier:#"SampleCell2"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2 == 0) {
SampleCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SampleCell1" forIndexPath:indexPath];
// populate cell with data
return cell;
}else{
SampleCell *cell = [tableView dequeueReusableCellWithIdentifier:#"SampleCell2" forIndexPath:indexPath];
// populate cell with data
return cell;
}
}
This is all you need to do. There's no need to check for a nil cell, because dequeueReusableCellWithIdentifier:forIndexPath: is guaranteed to return a valid cell if you either register a nib, or make the cell in a storyboard (in which case you don't need to register anything).
I have a UITableView that holds an FBProfilePictureView and a UILable,
my problem is when a user scrolls the picture rebuilds its self and it takes time to show the image it self, i want to know how can i create the FBProfilePictureView once and then when the user scrolls it won't build it self again.
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
Messages *msg = [messages objectAtIndex:indexPath.row];
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSData *encodedDataObject = [def objectForKey:#"myUser"];
User *user = (User *)[NSKeyedUnarchiver unarchiveObjectWithData: encodedDataObject];
if(![msg.wrote_id isEqualToString:user.fid])
{
NSString *idToImage = [self getOtherUserId];
NSString *CellIdentifier = #"Cell";
ChatWindowCell *cell = nil;
cell = (ChatWindowCell *)[tableChat dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ChatWindowCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[ChatWindowCell class]])
{
cell = (ChatWindowCell *) currentObject;
cell.profile_image.profileID=nil;
if(cell.profile_image.profileID.length>0)
{
return cell;
}
break;
}
}
cell.profile_image.profileID = idToImage;
NSString *text = msg.msg;
cell.lblMessage.text=text;
return cell;
}
else
{
NSString *CellIdentifier = #"Cell";
ChatCellMe *cell = nil;
cell = (ChatCellMe *)[tableChat dequeueReusableCellWithIdentifier:CellIdentifier];
// if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"ChatCellMe" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[ChatCellMe class]])
{
cell = (ChatCellMe *) currentObject;
NSLog(cell.prof_img.profileID);
cell.prof_img.profileID=nil;
if(cell.prof_img.profileID.length>0)
{
return cell;
}
break;
}
}
cell.prof_img.profileID = user.fid;
NSString *text = msg.msg;
cell.lblText.text=text;
isMessageFromMe=false;
return cell;
}
}
any help would be great guys...
thanks a lot.
I created an alternate view to solve this problem, see https://github.com/combinatorial/DBFBProfilePictureView