i can draw a string/image… with drawInRect/drawAtPoint.
but how about if i want to add a UIProgressView into this view?
I am overriding the drawRect method,
so i have to create the UIProgressView inside drawRect?
UPDATED BELOW:
Here is my UITableViewCell subclass for another class(just for showing how i did the tableViewCell subclassing) for Krumelur, I am not sure if this is correct, but this is how i usually do it.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5.0f,5.0f,40.0f,40.0f)];
//imageView.tag = kImage;
imageView.layer.cornerRadius = 5.0;
imageView.layer.masksToBounds = YES;
[self.contentView addSubview:imageView];
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(55.0f, 5.0f, 220.0f, 18.0f)];
//nameLabel.tag = kName;
nameLabel.font = [UIFont boldSystemFontOfSize:16];
nameLabel.textColor = [[UIColor alloc] initWithRed:18.0/255.0 green:18.0/255.0 blue:18.0/255.0 alpha:1.0f];
//nameLabel.textColor = [UIColor blackColor];
nameLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:nameLabel];
//[nameLabel release];
statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(55.0f, 27.0f, 220.0f, 20.0f)];
//lastMessageLabel.tag = kName;
statusLabel.font = [UIFont systemFontOfSize:13];
statusLabel.textColor = [UIColor colorWithRed:82.0/255.0
green:82.0/255.0
blue:82.0/255.0
alpha:0.8];;
statusLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:statusLabel];
//[lastMessageLabel release];
}
return self;
}
here is how i use it in -(void)cellForRowAtIndexPath
static NSString *CellIdentifier = @"RootViewTableCell";
RootViewTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[RootViewTableCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
//cell.backgroundView.backgroundColor = TABLE_BACKGROUND_COLOR;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
User *user = [rows objectAtIndex:indexPath.row];
cell.nameLabel.text = user.name;
cell.statusLabel.text = user.status;
if (user.photo != nil)
{
//myUser.photo = user.photo;
cell.imageView.image = user.photo;
}
else{
cell.imageView.image = [UIImage imageNamed:@"person_default"];
}
return cell;
do i have to use a tag for each of the subview? i don’t see any difference if i don’t.
do you see anything that is causing the tableview scrolling lag? because I use the same thing for my tableview before. the table datasource is from core data, and NSFetchedResultsController for any insert/delete/update, but i am sure that i store the data into a NSMutableArray as datasource, so when i scroll the tableview, there is no core data access(but still laggy). Please if you have any suggestion, i would love to hear, and i love this way more than doing UIView subclassing then override the drawRect.
thanks
You should not draw UIProgressView in a UITableViewCell. The reason why you gain performance by overriding a UITableViewCell’s drawRect is because the device only has to composite the first frame of scrolling. The rest of the time, it’s a single opaque texture.
Now you know why you gain performance, you can see that you can’t draw UIProgressView into drawRect, because you’re going to be updating that UIProgressView, which results in slow performance. The drawRect method is only for static content and not meant for cells that have animation.
The best thing to do is to addSubview, but don’t add the UIProgressView as a subview in
cellForRowAtIndexPath. This will result in a lot of duplicate UIProgressViews overlapping each other (due to the cell reuse).What you want to do is (since you’re already subclassing UITableViewCell) create a UIProgressView property in the UITableViewCell subclass, add it to the cell in the initWithStyle method, then rearrange it in the layoutSubviews.