Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

erdinc27

macrumors regular
Original poster
Jul 20, 2011
168
1
i use custom UITableViewCell in a UITableView. In this custom cell i have a UIView(renkView) which contains an UILabel(haberCatLbl). i change the width and position of that UIView according to UILabel's width. but the problem that i face is whenever i run the application first two cells which are visible cells doesn' t change that UIView's position and width but whenever i scroll the UITableView down and up then the UIView takes the frame that it has to. what can be wrong here ? here my codes are.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"HaberCell";
    
    HaberCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (cell == nil)
    {
        cell = [[HaberCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    
    Haber *currentHaber = [self.arrHaberler objectAtIndex:indexPath.row];
    
    UIColor *bgColor = [self.arrColors objectAtIndex:(indexPath.row) % 7];
    
    NSString *categoryTitle = currentHaber.haberCategory;

    CGSize titleLength = [categoryTitle sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:10] constrainedToSize:CGSizeMake(9999, 20)];
    
    cell.renkView.frame = CGRectMake(306 - (titleLength.width+10), 16, titleLength.width+10, 20);
    
    cell.renkView.backgroundColor = bgColor;
    
    cell.haberCatLbl.text = currentHaber.haberCategory;
    cell.haberCatLbl.font = [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:10];

    cell.haberCatLbl.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)viewDidAppear:(BOOL)animated
{
    
    [self.tblHaberler reloadData];
    
    [super viewDidAppear:animated];
}

beside that i used layoutSubviews method in HaberCell.m file to resize the View but still same problem. how can i fix that problem ?
 
What does your custom initWithStyle look like? Could it be that when you call initWithStyle (when there are no cells to dequeue) it is just returning a normal UITableViewCell?

Also, just a note, I believe it is common practice to call the

Code:
[super viewDidAppear:animated];

before you call anything else in that method
 
What does your custom initWithStyle look like? Could it be that when you call initWithStyle (when there are no cells to dequeue) it is just returning a normal UITableViewCell?

He's allocating and initializing his custom cell so (from the code he is showing) I don't see how he could be getting anything other than his custom table view cell.

What I suspect is happening is this:
The first cells are created but the values are not set until after the frames are set. One of the cells gets added to the queue. When the cell gets re-used, it has values in it so the frames are then set correctly.

I suggest this, and I offer no guarantees:
Override the setter methods for the custom label so that the frame gets updated after the value is set.

And it may not hurt to implement
Code:
-(void)prepareForReuse
 
@waterskier2007 it returns only HaberCell object. nothing else.

@TheWatchfulOne can u explain what u meant with an example if it is possible please ? I am so confused. Many times i used custom cell but that is the first time i face with such a problem.
 
@TheWatchfulOne can u explain what u meant with an example if it is possible please ? I am so confused. Many times i used custom cell but that is the first time i face with such a problem.

OK, but can you post the contents of HaberCell.h and HaberCell.m so we know exactly what we're working with?
 
OK, but can you post the contents of HaberCell.h and HaberCell.m so we know exactly what we're working with?

here is HaberCell.h
Code:
@interface HaberCell : UITableViewCell

@property (retain, nonatomic) IBOutlet UILabel *haberLbl;
@property (weak, nonatomic) IBOutlet UILabel *haberCatLbl;
@property (weak, nonatomic) IBOutlet UIView *renkView;
@property (weak, nonatomic) IBOutlet AsyncImageView *hbrImage;

@end

and HaberCell.m
Code:
@implementation HaberCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
    {

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

//- (void)layoutSubviews
//{
//    [super layoutSubviews];
//    
//    self.renkView.frame = CGRectMake(0, 0, self.frame.size.width, 100);
//}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.