I have a Static UITableView with 4 custom Cells. As the cell's are static so it refers that the whole thing is in UITableViewController. I put some random image inside the cells. Now I want to keep my first cell fixed at the top of my tableView. And the rest three cell will be scrolled. I can't do it by putting an extra UIImageView top of the UITableView, because it is static cell. Then, How can I do that?
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
if ((indexPath.row)==0)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"one"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
backgroundView.image=[UIImage imageNamed:#"default-cover-image.jpg"];
[cell.contentView addSubview:backgroundView];
}
if ((indexPath.row)==1)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"two"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
backgroundView.image=[UIImage imageNamed:#"sectionHeader.png"];
[cell.contentView addSubview:backgroundView];
}
if ((indexPath.row)==2)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"three"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
backgroundView.image=[UIImage imageNamed:#"sectionHeader2.png"];
[cell.contentView addSubview:backgroundView];
}
if ((indexPath.row)==3)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"four"];
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
backgroundView.image=[UIImage imageNamed:#"default-cover-image.jpg"];
[cell.contentView addSubview:backgroundView];
}
return cell;
}
Cells are made to be scrolled with the others, if you have a plain style table view you can simply use a header for the section.
If you have one section all the cells will be scrolled under the header view.
To make an easy experiment just add this code
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
And make it returns a string. If you need more customization use this delegate method:
- (UIView *)tableView:(UITableView *)tableView
viewForHeaderInSection:(NSInteger)section
I am used to set another UIViewController containing one UIImageView and one UITableViewController. Just like following:
You can choose View Container in Object Library, just like that:
After running, it's like this:
To accomplish what you described you got 2 options.
If you are using storyboards it's very simple. Just add a view above the tableView with the height of the cell you want and use autolayout to keep them together, like so:
you can see the lower yellow view, it acts like the header of the tableView but it will not scroll down once the user scrolls the table View down
if you are not using storyboard you will have to create the view by code, or create a header for your tableView. The only problem with headers is that they will scroll downwards if the user scroll down
I don't know is it possible or not. But I find another solution to accomplish the task. Here, The static cells are scrolling as usual but I put some extra space top of the tableView and put an UIImage in that empty position.
Set the frame of my Static tableView with CGRectMake(0, 170, 320, 960):
[self.tableView setFrame:CGRectMake(0, 170, 320, 960)];
It put some black space in that empty space. So I put the background as white.
[[[UIApplication sharedApplication] keyWindow] setBackgroundColor:[UIColor whiteColor]];
Then add an UIImageView in that empty space (Here you can't add UIImage in tableView, you have to add that image in superView).
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
backgroundView.image=[UIImage imageNamed:#"default-cover-image.jpg"];
[self.view.superview addSubview:backgroundView];
So the code will looks like this:
- (void)viewDidAppear:(BOOL)animated
{
// Set the frame of the UITableViewController table
[self.tableView setFrame:CGRectMake(0, 170, 320, 960)];
[[[UIApplication sharedApplication] keyWindow] setBackgroundColor:[UIColor whiteColor]];
// Set the image in superView (not in tableView) as subView on top of the tabelView
UIImageView *backgroundView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
backgroundView.image=[UIImage imageNamed:#"default-cover-image.jpg"];
[self.view.superview addSubview:backgroundView];
}
Try to use a normal cell and use it as a header while the other you index them + 1 for displaying
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrayPeople count]<1 ? 0 : arrayPeople.count-1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(arrayPeople.count>0)
{
RankingCell *cell =nil;
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RankingCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
if ([currentObject isKindOfClass:[RankingCell class]])
{
cell = (RankingCell *)currentObject;
break;
}
NSDictionary *element=[arrayPeople objectAtIndex:0];
element=[element dictionaryByReplacingNullsWithStrings];
[cell setupCell:[element objectForKey:#"username"] title2:[element objectForKey:#"total"] title3:[element objectForKey:#"place"] remoteImage:[element objectForKey:#"image"] forPath:[NSIndexPath indexPathForItem:0 inSection:0]];
return cell;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RankingCell";
RankingCell *cell =(RankingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RankingCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
if ([currentObject isKindOfClass:[RankingCell class]])
{
cell = (RankingCell *)currentObject;
break;
}
}
NSDictionary *element=[arrayPeople objectAtIndex:indexPath.row+1];
element=[element dictionaryByReplacingNullsWithStrings];
[cell setupCell:[element objectForKey:#"username"] title2:[element objectForKey:#"total"] title3:[element objectForKey:#"place"] remoteImage:[element objectForKey:#"image"] forPath:[NSIndexPath indexPathForItem:indexPath.row+1 inSection:0]];
return cell;
}
Related
in my cell data is loading but its is all messed up until i scroll down then if i scroll back everything format correctly. so when every i load listing which is uitableview data is messed up and as soon i start scrolling and scroll back up it gets in place and show correctly. please help.
my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
for(UIView *v in [cell subviews])
{
if([v isKindOfClass:[UILabel class]])
[v removeFromSuperview];
if ([v isKindOfClass:[UIImageView class]])
[v removeFromSuperview];
}
UIView *CellBGView = [[UIView alloc] initWithFrame:CGRectMake(10, 5, cell.frame.size.width-20, cell.frame.size.height-10)];
CellBGView.backgroundColor = [UIColor whiteColor];
UIImageView *divider = [[UIImageView alloc] initWithFrame:CGRectMake(CellBGView.frame.size.width-155, CellBGView.frame.size.height-140, 150, 150)];
divider.image = [UIImage imageNamed:#"icon_alpha.png"];
[CellBGView addSubview:divider];
......
return cell;
}
Here is out put when i open listing and before scrolling table
here is after scrolling everything gets in place
I have a solution for you.
First, create subclass of UITableViewCell (For example, name it DemoTableViewCell)
In initWithStyle method of DemoTableViewCell, add your images and labels to cell.
In cellForRowAtIndexPath, you don't need add or remove images and labels. Just change color or set image for them.
I know this question is already asked many times, but my problem is some different.
I am creating a UIView and a UIImageView programmatically in cell's content view. When TableView appear first time it looking perfect, but when i scroll down and up , this seems overlapped.
Screenshot of without scroll:
Screenshot after scroll:
Code that i follow:
viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.viewForContents.frame.origin.x, cell.viewForContents.frame.origin.y-10, cell.viewForContents.frame.size.width, 45)];
viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:1];
[cell.contentView addSubview:viewForHead];
UIImageView *imageViewForDP = [[UIImageView alloc]initWithFrame:CGRectMake(viewForHead.frame.origin.x-50, viewForHead.frame.origin.y-8, 60,60 )];
imageViewForDP.image = [UIImage imageNamed:#"dog_1.png"];
//[cell.viewForContents addSubview:imageViewForDP];
imageViewForDP.layer.cornerRadius = 30;
imageViewForDP.clipsToBounds = YES;
[viewForHead addSubview:imageViewForDP];
Please get me out from this problem . Thanks
Use this into your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if ([cell.contentView subviews]){
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
}
You are adding your viewForHead as a subview each time the cell gets dequeued. So you're adding them on top of each other.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CELL"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"CELL"] autorelease];
// This is where you CREATE your cell contents.
viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.viewForContents.frame.origin.x, cell.viewForContents.frame.origin.y-10, cell.viewForContents.frame.size.width, 45)];
viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:1];
[cell.contentView addSubview:viewForHead];
UIImageView *imageViewForDP = [[UIImageView alloc]initWithFrame:CGRectMake(viewForHead.frame.origin.x-50, viewForHead.frame.origin.y-8, 60,60 )];
imageViewForDP.image = [UIImage imageNamed:#"dog_1.png"];
// [cell.viewForContents addSubview:imageViewForDP];
imageViewForDP.layer.cornerRadius = 30;
imageViewForDP.clipsToBounds = YES;
imageView.tag = 1
[viewForHead addSubview:imageViewForDP];
}
// this is where you UPDATE your viewForHead image and any other elements inside your cell
UIImageView *imageView = [cell.contentView viewWithTag:1];
imageView.image = // your new image
return cell;
}
Subclassing your UITableViewCell and building your layout with a xib would be even better, then you could just access the cells properties directly. A much cleaner solution.
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier#"CELL"];
if (cell == nil) {
cell = [[MyCustomCell alloc] init]; // you ID is set in interface builder
}
cell.imageView.image = // your new image here.
cell.someLabel.text = #"some new text here"
This problem is because of table view cell gets reuse and you are adding a view again on cell.
Try below code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier"] ;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
UIView *viewForHead = (UIView *)[cell.contentView viewWithTag:1];
if (viewForHead==nil) {
viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, 20)];
viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:0.5];
viewForHead.tag = 1;
[cell.contentView addSubview:viewForHead];
}
return cell;}
I am implementing a UIViewController that contains a TableView , I am trying to add a view created programmatically in TableViewCell , on scrolling the view hides and works strange .
anyone can help me
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
if(cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
UIView *BottomView=[[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height , self.view.frame.size.width, 50)];
BottomView.backgroundColor=[UIColor redColor];
[cell addSubview:BottomView];
return cell;
}
Bro change your BottomView frame and may be it will visible
UIView *BottomView=[[UIView alloc] initWithFrame:CGRectMake(0, 10 , self.view.frame.size.width, 50)];
BottomView.backgroundColor=[UIColor redColor];
[cell addSubview:BottomView];
Because you are setting the height of BottomView as self.view.frame.size.height which will put your view as beyond the frame of your cell so change it and it will work.
change
[cell addSubview:BottomView];
to
[cell.contentView addSubview:BottomView];
I seem to be having a problem understanding how subviews work. I have a UITextfield and I want to add this to the content view of a UITableViewCell so i do this:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
} // Configure the cell...
UITextField *tf = [textFieldsArray objectAtIndex:[indexPath row]];
tf.frame = CGRectMake(0, 0, 100, 12);
[cell.contentView addSubview:tf];
return cell;
}
I assumed that by making the textfields frames origin (0,0) when i add this as a subview to the content view, it will place it in the content view at position (0,0), but this is not happening, it is actually placing it at position (0,0) of the entire cell (the top left most part of the cell).
Unless this has been changed in iOS7, i thought the content view was like this:
(source: idev101.com)
If so, is there a way i can actually place this in the contentView without having to manually define an offset each time?
Try This
if (cell==nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
UIImageView *img=[[UIImageView alloc]init];
img.tag=100;
img.layer.cornerRadius = 3;
img.layer.masksToBounds = YES;
[cell addSubview:img];
UILabel *lbl= [[UILabel alloc]init];
lbl.backgroundColor= [UIColor clearColor];
lbl.text=#"ABC";
lbl.textAlignment=NSTextAlignmentLeft;
lbl.textColor=[UIColor blackColor];
lbl.tag=200;
[cell addSubview:lbl];
}
img1=(UIImageView *)[cell viewWithTag:100];
NSArray *imgDataArr = [[NSArray alloc] initWithArray:[[arrFields objectAtIndex: storyIndex] objectForKey: #"image"]];
for (UIImage *image in imgDataArr)
{
if (image)
img1.image =image;
}
[cell addSubview:img1];
UILabel *lbl1 = (UILabel*)[cell viewWithTag:200];
[cell addSubview:lbl1];
lbl1.numberOfLines=3;
lbl1.text=[[arrFields objectAtIndex: storyIndex] objectForKey: #"Item_No"];
I used the following code to implement cell for every row at index path:
But the problem is when I scroll the tableView, the cell will load a lot of UIImageView *itemimageview in one cell in one line, I tried use
for (UIImageView *sView in cell.subviews) {
[sView removeFromSuperview];
}
but it would remove all subviews of one cell. How to solve this problem?...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
NSUInteger oldRow = [lastIndexPath row];
static NSString *CheckMarkCellIdentifier = #"CheckMarkCellIdentifier";
//dequeueReusableCellWithIdentifier --
// Returns a reusable table-view cell object located by its identifier.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CheckMarkCellIdentifier] autorelease];
}
/*
for (UIImageView *sView in cell.subviews) {
[sView removeFromSuperview];
}
*/
UIImageView *itemimageview=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 232, 54)];
itemimageview.image=[UIImage imageNamed:[tabsImageArray objectAtIndex:row]];
itemimageview.userInteractionEnabled = YES;
[cell.contentView addSubview:itemimageview];
[itemimageview release];
UIImageView *dictIcon=[[UIImageView alloc]initWithFrame:CGRectMake(30, 18, 30, 30)];
dictIcon.image=[UIImage imageNamed:#"dictionary_icon.png"];
dictIcon.userInteractionEnabled = YES;
[cell.contentView addSubview:dictIcon];
[dictIcon release];
UILabel *dictNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 23, 100, 21)];
dictNameLabel.text = dictName;
dictNameLabel.textColor = [UIColor whiteColor];
dictNameLabel.shadowColor = [UIColor blackColor];
dictNameLabel.backgroundColor = [UIColor clearColor];
dictNameLabel.userInteractionEnabled = YES;
[cell.contentView addSubview:dictNameLabel];
[dictNameLabel release];
//cell.textLabel.text = [tabsImageArray objectAtIndex:row];
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
Consider running through and checking [view isKindOfClass:[UIImageView class]] to check whether the view is the right type of view to remove.
Also consider tagging the views with the UIView tag property, so you can add the subviews once, and then won't have to recreate them with reuse.
Here is how you would do this:
#define ImageViewOneTag 1001
#define ImageViewTwoTag 1002
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)path {
static NSString *CellID = #"CellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
UIImageView *imageViewOne = nil;
UIImageView *imageViewTwo = nil;
if (cell) {
// You've caught a reusable cell. Fetch the image views by their tag.
imageViewOne = (UIImageView *)[cell viewWithTag:ImageViewOneTag];
imageViewTwo = (UIImageView *)[cell viewWithTag:ImageViewTwoTag];
} else {
// You haven't got a reusable cell. Make one, and make and add the image views to the contentView.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
imageViewOne = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)];
[imageViewOne setTag:ImageViewOneTag];
imageViewTwo = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 20.0, 20.0, 20.0)];
[imageViewTwo setTag:ImageViewTwoTag];
UIView *contentView = cell.contentView;
[contentView addSubview:imageViewOne];
[contentView addSubview:imageViewTwo];
}
// By this stage, you've either retrieved a reusable cell, or you've made a new one. Either way, imageViewOne and imageViewTwo now have a reference to the views you mean.
imageViewOne.image = *imageOneForRow*;
imageViewTwo.image = *imageTwoForRow*;
return cell;
}
I suggest you using custom table view cell, just create a cocoa touch class which inherits uitableviewcell.