I have two different kind of cell layouts in my tableView.One for normal height and other for expanded height.Both of them are made in XIB.The problem is that the expanded cell is overwriting data.(For example the labels are set with some text which keeps on adding on top of previous text as I go on expanding different cells).
Here's how the cells are loaded.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = #"Cell";
static NSString *expandedCellIdentifier = #"ExpandedCell";
if (!isExpanded)
{
ListCell *cell =(ListCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil)
{
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:#"ListCell" owner:self options:nil];
cell = nibs[0];
}
cell.Name.text = [[bArray objectAtIndex:indexPath.row]valueForKey:#"Name"];
return cell;
}
else
{
expCell =(ExpandedCell*) [tableView dequeueReusableCellWithIdentifier:expandedCellIdentifier];
if (expCell==nil)
{
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:#"ExpandedCell" owner:self options:nil];
expCell = nibs[0];
}
UILabel *jTime = [[UILabel alloc]initWithFrame:CGRectMake(27, 6, 72, 10)];
jTime.text = [NSString stringWithFormat:#"%#",timeRequired];
[expCell.background_View addSubview:jTime];
return expCell;
}
return nil;
}
Your label gets added every time when the cell is reused. Use this code in else part of your if condition to avoid that:
expCell = (ExpandedCell*) [tableView dequeueReusableCellWithIdentifier:expandedCellIdentifier];
if (expCell == nil)
{
NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:#"ExpandedCell" owner:self options:nil];
expCell = nibs[0];
UILabel *jTime = [[UILabel alloc]initWithFrame:CGRectMake(27, 6, 72, 10)];
[jTime setTag:100]; //or any value
[expCell.background_View addSubview:jTime];
}
UILabel *jTimeLabel = (UILabel *)[expCell.background_View viewWithTag:100];
jTimeLabel.text = [NSString stringWithFormat:#"%#",timeRequired];
return expCell;
Through this code, your label will get added only once.
You are adding new subview to your expcell every time it's dequeued.
Because of these lines:
UILabel *jTime = [[UILabel alloc]initWithFrame:CGRectMake(27, 6, 72, 10)];
jTime.text = [NSString stringWithFormat:#"%#",timeRequired];
[expCell.background_View addSubview:jTime];
That will add new UILabel everytime the cell is reused.
Related
Normally in iOS, the cells in a tableview are loading on scrolling. In this time I'm setting a label text. In this case I'm setting this text dynamically. When I'm scrolling down it's working as I expected. But when It's scrolling up, it's not working as expected. Here what I tried.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"moduleCell";
DateTableViewCell *cell = (DateTableViewCell*)[tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"DateTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#""]];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if (self.newsSegmentControl.selectedSegmentIndex == 1) {
[cell.dateView setHidden:NO];
}else{
[cell.dateView setHidden:YES];
}
NSString *day = [self getDayLabel:date];
if ([dateReturned isEqualToString:day]) {
[cell.lblDate setText:#""];
if (self.newsSegmentControl.selectedSegmentIndex == 1) {
[cell.dateView setHidden:YES];
}
}else{
[cell.lblDate setText:day];
dateReturned = day;
}
}
How may I avoid re-draw the label text in cell on scrolling?
I am new to developing. i have some experience but i'm not great at it (yet). I'm working on this tableview app which shows data. I want to show that data in a custom cell. But i don't know how to do that.
This is the code i use for 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 line of code in my custom cell.
cell.textLabel.text = [tempDictionary objectForKey:#"PickupAddress"];
I already made a class and a xib file for the custom cell
This may solve the problem:
HistoryTableViewCell *cell = (HistoryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"HistoryViewTableCell"];
your written kind of code will not work. if you are using custom XIB than add a UILable in the xib. and than assign them tag number.
now from ViewController do the following:
write this after #implementation
enum {
CELL_TITLE = 2,
CELL_SUBTITLE_1 = 3,
};
Load the Custom XIB File in cellforrowatindexpath method.
// Get table View Cell
static NSString *CellIdentifier = #"customCell";
UITableViewCell *cell = [atableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib;
nib = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = (UITableViewCell *) [nib objectAtIndex:0];
}
Now Add text on Lable with the below methods;
UILabel *titleLable = (UILabel *) [cell.contentView viewWithTag:CELL_TITLE];
titleLable.text = #"This is your text";
When I set the red part on the top cell ,the content of next part which is the green part doesn't display completely or doesn't display.Only when I select the green part ,the image will appear .If I put the one cell of green above the red part ,it will display and the next part which is under the red part doesn't appear.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0) {
static NSString *identifyCell = #"TopCell";
TopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifyCell];
if (!cell) {
cell = (TopTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:#"TopTableViewCell"
owner:self options:nil] objectAtIndex:0];
//cell.lbValue.hidden = YES;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//config the cell
return cell;
}
else{
static NSString *identifyCell = #"HomeCell";
HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifyCell];
if (!cell) {
cell = (HomeTableViewCell *)[[[NSBundle mainBundle] loadNibNamed:#"HomeTableViewCell"
owner:self options:nil] objectAtIndex:0];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//cell.lbValue.hidden = YES;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
cell.homeimg.image = [UIImage imageNamed:#"home1.jpg"];
//config the cell
return cell;
}
}
I have a UITableView with a Custom Cell. Some custom cells are being loaded with an image.
And I'm adding a custom separator for the cells that have an image. The problem arises on scrolling. Whenever I scroll, the UIView opacity changes.
Here is my code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
if (cell && (NSNull*) imageString != [NSNull null])
{
UIView* separatorLineView; = [[UIView alloc] initWithFrame:CGRectMake(0, -1, 320, 5)];
separatorLineView.backgroundColor = [UIColor blackColor];
separatorLineView.alpha = 0.2;
[cell.contentView addSubview:separatorLineView];
}
Alpha for the view given is 0.2 and on scrolling it becomes thicker and thicker. How can I solve this issue?
Do what #rmaddy and #rdelmar have suggested but since you're using a custom cell anyways, the quickest way, imho, would be:
Add this line to the cell's contentView via the Interface Builder directly
#property (strong, nonatomic) IBOutlet UIView *vwLine;
also #synthesize vwLine;
Set it's frame, background color & alpha value (obviously)
Set it's hidden property to YES
When you do the imageString != [NSNull null] check
if TRUE then [cell.vwLine setHidden:NO];
Example:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell"
owner:self
options:nil];
cell = [topLevelObjects objectAtIndex:0];
}
if (imageString != [NSNull null]) {
[cell.vwLine setHidden:NO];
}
}
I think the problem is that you're adding a separatorLineView to cells that already have one as you scroll, so what you're seeing is lines on top of other lines. You could fix that by adding a tag to your line view, and checking if the cell has a subview with that tag, and if so, don't add another line.
In your code might be problem of reusability of cell. So try with following code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
.
.
.
.
if(cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
if(cellHasImage == YES)
{
//// just initialize UIView and add as addSubview of cell.contentView
UIView* separatorLineView; = [[UIView alloc] init];
separatorLineView.frame = CGRectMake(0, -1, 320, 5);
separatorLineView.tag = 111; // set whatever you like
[cell.contentView addSubview:separatorLineView];
}
}
.
.
.
/// get UIView base on it's tag
if(cellHasImage == YES)
{
UIView* separatorLineView; = (UIView *) [cell.contentView viewWithTag:111];
separatorLineView.backgroundColor = [UIColor blackColor];
separatorLineView.alpha = 0.2;
}
.
.
.
return cell;
}
In cellForRowAtIndexPath I am adding a UIImageView to cell.contentView. The problem is that when the cell scrolls off the screen and back on, it adds the same image again on top of the one that is already there. This happens continuously until I get a very stacked-up blurry image.
Do you have to keep removing any image views that you add to cell.contentView? If so, in what delegate method do you do that in?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]];
imageView.center = CGPointMake(310, 48);
[cell.contentView addSubview:imageView];
[imageView release];
return cell;
}
If you don't want to keep putting imageViews in your cell, you have to do all the customization inside the if(cell==nil) block, otherwise it will add one every time a cell recycles. When using cell recycling you always want to keep anything that is consistent for all of your cells in that block so that they only get added once.
Ex:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
//Add custom objects to the cell in here!
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.jpg"]];
imageView.center = CGPointMake(310, 48);
[cell.contentView addSubview:imageView];
[imageView release];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
If each cell should need a different image, you can try something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
static const int ImageViewTag = 1234; //any integer constant
MyTableCell *cell = (MyTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIImageView *imageView;
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MyTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
//Add custom objects to the cell in here!
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, imgWidth, imgHeight)];
imageView.center = CGPointMake(310, 48);
imageView.tag = ImageViewTag;
[cell.contentView addSubview:imageView];
[imageView release];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else
{
imageView = [cell viewWithTag:ImageViewTag];
}
imageView.image = yourUIImageForThisCell;
return cell;
}