I have a simple but custom section header in which I am showing dynamic content in a label. However, when I pop a VC and in the active VC I run [self.quickDialogTableView reloadData] in viewWillAppear, the data is not being updated to the correct "self.sectionHeaderTitle. Any idea what I am doing wrong?
(void)sectionHeaderWillAppearForSection:(QSection *)section atIndex:(NSInteger)indexPath {
if (indexPath == 0) {
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 320, 110)];
header.backgroundColor = [UIColor clearColor];
UIView *header_background = [[UIView alloc] initWithFrame:CGRectMake(25, 10, 270, 100)];
header_background.backgroundColor = [UIColor whiteColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 255, 25)];
title.text = #"A sample static title";
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(55, 40, 200, 40)];
label.text = self.sectionHeaderTitle;
[header addSubview:header_background];
[header_background addSubview:title];
[header_background addSubview:label];
[section setHeaderView:header];
}
Try reload the section, like this:
[self.quickDialogTableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationBottom];
Related
I have a scenario where my Table holds list of products. This list has a footerView with a button added to the footerView. I could able to see/scroll complete footerView along with the button when I open the already created Product List. But I cannot see complete button in the footerView when I create & Save the Product list for the first time.
I am using below code:
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE_WIDTH, 44)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10, 0, SCREEN_SIZE_WIDTH - 20, 44);
[footerView addSubview:button];
self.tableView.tableFooterView = footerView;
[self.tableView setTableFooterView:footerView];//Tried this one too
Please help me to show/scroll the button in footerView. Thanks!
Use this code, its working fine for me:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
if(array.count>1){
if(indexPath.row == array.count-1){
[self setupTableViewFooter];
}
}
}
- (void)setupTableViewFooter
{
// set up label
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
footerView.backgroundColor = [UIColor clearColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.font = [UIFont fontWithName:Font_MuseoSans size:14];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.textColor = [UIColor darkGrayColor];
label.text = #"Loading";
self.footerLabel = label;
CGSize labelSize = [self.footerLabel sizeThatFits:footerView.frame.size];
[footerView addSubview:self.footerLabel];
// set up activity indicator
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicatorView.hidesWhenStopped = YES;
activityIndicatorView.color = [UIColor blackColor];
self.activityIndicator = activityIndicatorView;
[self.activityIndicator startAnimating];
[footerView addSubview:self.activityIndicator];
CGRect footerFrame = footerView.frame;
label.frame = CGRectMake((footerFrame.size.width-labelSize.width - 4 - activityIndicatorView.frame.size.width)/2, (footerFrame.size.height-labelSize.height)/2
, (footerFrame.size.width-labelSize.width - 4 - activityIndicatorView.frame.size.width), labelSize.height);
self.activityIndicator.frame = CGRectMake(label.frame.origin.x + labelSize.width + 4, (footerFrame.size.height-activityIndicatorView.frame.size.height)/2
, activityIndicatorView.frame.size.width, activityIndicatorView.frame.size.height);
self.tableView.tableFooterView = footerView;
}
I have a UITableView with only 1 section title that contains labels when i reload data some of the label of the section may be hidden. But when I do reload data they are redraw in section I just wanted to know if there is some sort of dequeueReusableCellWithIdentifier: to avoid this like for the cell but for secions.
Here is the code
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 36)];
/* Create custom view to display section header... */
UILabel *labelSeq = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, 30, 18)];
[labelSeq setFont:[UIFont boldSystemFontOfSize:12]];
labelSeq.text = #"SEQ";
UILabel *labelSup = [[UILabel alloc] initWithFrame:CGRectMake(50, 2, 330, 18)];
[labelSup setFont:[UIFont boldSystemFontOfSize:12]];
labelSup.text = #"SUPPLIER";
UILabel *labelMod = [[UILabel alloc] initWithFrame:CGRectMake(390, 2, 150, 18)];
[labelMod setFont:[UIFont boldSystemFontOfSize:12]];
labelMod.text = #"MODEL";
UILabel *labelFinish = [[UILabel alloc] initWithFrame:CGRectMake(550, 2, 150, 18)];
[labelFinish setFont:[UIFont boldSystemFontOfSize:12]];
labelFinish.text = #"FINISH";
UILabel *labelQty = [[UILabel alloc] initWithFrame:CGRectMake(710, 2, 30, 18)];
[labelQty setFont:[UIFont boldSystemFontOfSize:12]];
labelQty.text = #"QTY";
UILabel *labelSale = [[UILabel alloc] initWithFrame:CGRectMake(750, 2, 125, 18)];
[labelSale setFont:[UIFont boldSystemFontOfSize:12]];
labelSale.textAlignment = UITextAlignmentRight;
labelSale.text = #"SALE PRICE";
labelSale.tag = 100;
UILabel *labelCost = [[UILabel alloc] initWithFrame:CGRectMake(885, 2, 125, 18)];
[labelCost setFont:[UIFont boldSystemFontOfSize:12]];
labelCost.textAlignment = UITextAlignmentRight;
labelCost.text = #"COST";
labelCost.tag = 200;
UILabel *labelDesc = [[UILabel alloc] initWithFrame:CGRectMake(50, 17, 700, 18)];
[labelDesc setFont:[UIFont boldSystemFontOfSize:12]];
labelDesc.text = #"DESCRIPTION";
UILabel *labelUnit = [[UILabel alloc] initWithFrame:CGRectMake(750, 17, 125, 18)];
[labelUnit setFont:[UIFont boldSystemFontOfSize:12]];
labelUnit.textAlignment = UITextAlignmentRight;
labelUnit.text = #"UNIT PRICE";
labelUnit.tag = 300;
UILabel *labelProfit = [[UILabel alloc] initWithFrame:CGRectMake(885, 17, 125, 18)];
[labelProfit setFont:[UIFont boldSystemFontOfSize:12]];
labelProfit.textAlignment = UITextAlignmentRight;
labelProfit.text = #"PROFIT";
labelUnit.tag = 400;
[view addSubview:labelSeq];
[view addSubview:labelSup];
[view addSubview:labelMod];
[view addSubview:labelFinish];
[view addSubview:labelQty];
[view addSubview:labelSale];
[view addSubview:labelCost];
[view addSubview:labelDesc];
[view addSubview:labelUnit];
[view addSubview:labelProfit];
[view setBackgroundColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]]; //your background color...
_headerView = view;
return view;
}
Add a property to your class to hold on to that UIView. Then in -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section, just check to see if that property is nil or not. If it's nil, create it and set that property to it, otherwise just use the UIView saved in the property. If it requires updating it some, then you can do that at that point. It looks like all your content is static, so you should never need to update the view at all after the first time you create it.
I have a UITableView with a header. the problem I currently have is that the header doesn't scroll with the table. I need it to scroll off screen (above) when the user scrolls the table view up. the tableview scrolls but the header is locked at the top of the UIView.
thanks
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionHeader = [[UILabel alloc] initWithFrame:CGRectNull];
sectionHeader.backgroundColor = [UIColor whiteColor];
// add user profile image to _contentView
UIImageView *userImageView;
UIImage *userImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:userProfileImageUrl]]];
userImageView=[[UIImageView alloc]initWithImage:userImage];
userImageView.frame=CGRectMake(10,10,90,100);
[sectionHeader addSubview:userImageView];
// return userImageView;
// user name lable
CGRect userNameFrame = CGRectMake(110, 60, 100, 50 );
UILabel* userNameLabel = [[UILabel alloc] initWithFrame: userNameFrame];
[userNameLabel setText: firstName];
[userNameLabel setTextColor: [UIColor blackColor]];
[userNameLabel setBackgroundColor:[UIColor clearColor]];
[userNameLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:14]];
[sectionHeader addSubview:userNameLabel];
// user last name label
CGRect userLastNameFrame = CGRectMake(110, 75, 100, 50 );
UILabel* userLastNameLabel = [[UILabel alloc] initWithFrame: userLastNameFrame];
[userLastNameLabel setText: lastName];
[userLastNameLabel setTextColor: [UIColor blackColor]];
[userLastNameLabel setBackgroundColor:[UIColor clearColor]];
[userLastNameLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:14]];
[sectionHeader addSubview:userLastNameLabel];
// user checkin view
UIView *userCheckinView = [[UIView alloc] initWithFrame:CGRectMake(10, 120, 280, 25)];
userCheckinView.backgroundColor = customColorGrey;
[sectionHeader addSubview:userCheckinView];
// check in label
UILabel* userCheckInLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 2, 100, 20)];
[userCheckInLabel setText: #"CHECK-IN"];
userCheckInLabel.backgroundColor = customColorGrey;
userCheckInLabel.textColor = customColorIt;
[userCheckInLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:12]];
[userCheckinView addSubview:userCheckInLabel];
// image
UIImageView *checkinImg = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"classifica_geotag_C.png"]];
checkinImg.frame = CGRectMake(5, 0, 24, 24);
[userCheckinView addSubview:checkinImg];
// check in label
UILabel* userCheckInCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, 2, 20, 20)];
[userCheckInCountLabel setText: [checkinCount stringValue]];
userCheckInCountLabel.backgroundColor = customColorGrey;
userCheckInCountLabel.textColor = customColorIt;
[userCheckInCountLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:12]];
[userCheckinView addSubview:userCheckInCountLabel];
// user like view
UIView *userLikeView = [[UIView alloc] initWithFrame:CGRectMake(10, 150, 280, 25)];
userLikeView.backgroundColor = customColorGrey;
[sectionHeader addSubview:userLikeView];
// like label
UILabel* userLikeLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 2, 100, 20)];
[userLikeLabel setText: #"LIKE"];
userLikeLabel.backgroundColor = customColorGrey;
userLikeLabel.textColor = customColorIt;
[userLikeLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:12]];
[userLikeView addSubview:userLikeLabel];
// image
UIImageView *likeImg = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"classifica_like_C.png"]];
likeImg.frame = CGRectMake(5, 0, 24, 24);
[userLikeView addSubview:likeImg];
// user like label
UILabel* userLikeCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(250, 2, 20, 20)];
[userLikeCountLabel setText: [likesCount stringValue]];
userLikeCountLabel.backgroundColor = customColorGrey;
userLikeCountLabel.textColor = customColorIt;
[userLikeCountLabel setFont:[UIFont fontWithName:#"DIN-Regular" size:12]];
[userLikeView addSubview:userLikeCountLabel];
// la mia bacheca like view
userLaMiaView = [[UIView alloc] initWithFrame:CGRectMake(10, 180, 300, 25)];
userLaMiaView.backgroundColor = [UIColor clearColor];
[sectionHeader addSubview:userLaMiaView];
// like label
UILabel* userLaMiaLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 150, 20)];
[userLaMiaLabel setText:NSLocalizedString(#"LA MIA BACHECA", nil)];
userLaMiaLabel.backgroundColor = [UIColor clearColor];
userLaMiaLabel.textColor = customColorGrey;
[userLaMiaLabel setFont:[UIFont fontWithName:#"DIN-Bold" size:10]];
[userLaMiaView addSubview:userLaMiaLabel];
// grey line view below la mia label
userGreyLineView = [[UIView alloc] initWithFrame:CGRectMake(10, 248, 280, 1.5)];
userGreyLineView.backgroundColor = [UIColor whiteColor];
[sectionHeader addSubview:userGreyLineView];
return sectionHeader;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 210;
}
That behavior is only common when the UITableViewStyle property of the table is set to UITableViewStylePlain. If you have it set to UITableViewStyleGrouped, the headers will scroll up with the cells.
This answer is taken from this question.
This solution works regardless of the number of headers.
create sectionHeader view in a new method and then add to the end:
self.tableView.tableHeaderView = sectionHeader;
if you have dynamic header view or single view select table style
Changing the UITableViewStyle from Plain to Grouped make the section header to scroll with the other cells.
If you write tableView.tableHeaderView = sectionHeader you will lose the actual table view header.
If you want to have table view header along with section headers you have to set UITableViewStyle property to UITableViewStyleGrouped.
Also if you want to calculate section header height automatically you can return UITableViewAutomaticDimension in heightForHeaderInSection method like this:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return UITableViewAutomaticDimension;
}
If you have a single header in the table then you can use tableHeaderView as below:
tableView.tableHeaderView = Header;
Or if you have multiple header in table than you need to use Group table instead of plain table.
Thanks
I am attempting to create a header label and to indent it so that it appears above the table view and not flush to the left side of the screen. I've implemented the following code, which is altering the appearance of the header label, but not the position.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,0, headerView.frame.size.width, 30)];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor grayColor];
headerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.text = #"Header Label";
[headerView addSubview:headerLabel];
return headerLabel;
}
Can anyone see what I am doing wrong?
Thanks
return ur headerView, not headerLabel. That will solve ur issue.
i suggest you to change this lines of code:
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,0, headerView.frame.size.width, 30)];
with:
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,30)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,0, 100, 30)];
then increments or decrements value 10 for padding left, and 0 for padding top...
hope help
I have a table going where each cell has a UIView in it that is fed from an XMLParser feed. If I leave it how it is, each time I refresh the views build upon each other because they are not getting released/removed, which makes total sense. How would I go about writing my code to avoid that problem, and not having to rebuild the view's each time I refresh. I am stuck in the logic of it and also the actual syntax. Thanks.
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
JointCAD *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];
self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"texture.png"]];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIView *selectionView = [[UIView alloc] initWithFrame:CGRectMake(10, 7, 200, 65)];
[selectionView setBackgroundColor: [UIColor clearColor]];
cell.selectedBackgroundView = selectionView;
}
// Display content in each cell
cellSeparator = [[UIView alloc] initWithFrame:CGRectMake(10, 7, 300, 65)];
[cellSeparator setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleWidth];
[cellSeparator setContentMode:UIViewContentModeTopLeft];
[cellSeparator setBackgroundColor: [UIColor colorWithRed:240.0/255.0 green:240.0/255.0 blue:240.0/255.0 alpha:1.0]];
cellSeparator.layer.borderWidth = 1.0;
cellSeparator.layer.borderColor = [UIColor blackColor].CGColor;
[cell addSubview:cellSeparator];
callTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 2, 190, 21)];
callTypeLabel.text = [currentCall currentCallType];
callTypeLabel.backgroundColor = [UIColor clearColor];
callTypeLabel.font = [UIFont boldSystemFontOfSize:12.0];
[cellSeparator addSubview:callTypeLabel];
locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 17 , 190, 15)];
locationLabel.text = [currentCall location];
locationLabel.backgroundColor = [UIColor clearColor];
locationLabel.font = [UIFont systemFontOfSize:10.0];
[cellSeparator addSubview:locationLabel];
unitsLabel = [[UILabel alloc]initWithFrame:CGRectMake(4, 43, 190, 21)];
unitsLabel.text = [currentCall units];
unitsLabel.backgroundColor = [UIColor clearColor];
unitsLabel.font = [UIFont italicSystemFontOfSize:10.0];
[cellSeparator addSubview:unitsLabel];
stationLabel = [[UILabel alloc]initWithFrame:CGRectMake(195 , 25, 75, 20)];
NSString *station = [#"Station: " stringByAppendingString:currentCall.station];
stationLabel.text = station;
stationLabel.backgroundColor = [UIColor clearColor];
stationLabel.font = [UIFont boldSystemFontOfSize:11.0];
[cellSeparator addSubview:stationLabel];
if ([currentCall.county isEqualToString:#"W"]) {
UIImage *countyImage = [UIImage imageNamed:#"blue.png"];
CGRect countyImageFrame = CGRectMake(275, 10, 18, 18);
UIImageView *countyImageLabel = [[UIImageView alloc] initWithFrame:countyImageFrame];
countyImageLabel.image = countyImage;
[cellSeparator addSubview:countyImageLabel];
} else {
UIImage *countyImage = [UIImage imageNamed:#"green.png"];
CGRect countyImageFrame = CGRectMake(275, 10, 18, 18);
UIImageView *countyImageLabel = [[UIImageView alloc] initWithFrame:countyImageFrame];
countyImageLabel.image = countyImage;
[cellSeparator addSubview:countyImageLabel];
}
if ([currentCall.callType isEqualToString:#"F"]) {
UIImage *callTypeImage = [UIImage imageNamed:#"red.png"];
CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
UIImageView *callTypeImageLabel = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageLabel.image = callTypeImage;
[cellSeparator addSubview:callTypeImageLabel];
} else {
UIImage *callTypeImage = [UIImage imageNamed:#"yellow.png"];
CGRect callTypeImageFrame = CGRectMake(275, 37, 18, 18);
UIImageView *callTypeImageLabel = [[UIImageView alloc] initWithFrame:callTypeImageFrame];
callTypeImageLabel.image = callTypeImage;
[cellSeparator addSubview:callTypeImageLabel];
}
return cell;
}
If I understand your question correctly, I think the solution you want is to subclass UITableViewCell, set up your subviews (UIImageViews and all that) and make them properties on your subclass. Then you can
if (!cell.imageView) {
cell.imageView = [[UIImageView alloc] initWithFrame:myFrame];
}
then just set the image. That way you're sure to not stack image views on top of one another and the correct image will get set when the cell is dequeued.
EDIT:
Make a class called MyTableViewCell (or whatever you want to call it but make sure it's a subclass of UITableViewCell)
Add #property (strong, nonatomic) UIImageView *imageView;
Then use MyTableViewCell instead of UITableViewCell in you cellForRowAtIndexPath and you can access the image property of your custom table view cell, check if it exists, if not create it (which is what the above code does) and then set the image. I would give you code but I'm on my phone. Google subclassing UITableviewCell. You should find a ton of examples.