UITableViewStyleGrouped with a right angle - iphone

When I create table view with
myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 400) style:UITableViewStyleGrouped];
table view has rounded corners, but I wanna have right angle (so table view cell should look like rectangles, but table view still should have many sections).
The question is: how can I create table view with many sections and right angles?

In the TableView datasource. In the cellForRowAtIndexPath you can set the background of the cell to be blank...
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero()];
Then you can put an image or rect or whatever in the back of the cell.
You could even create a new UIView and put that in place of the backgroundView.
Then you can put whatever you want in it, images, labels, etc... and they will go behind the content of the cell.
The rounded corners of the cell in grouped style is just an image so can be replaced by you.

set image as a background image to cell and give the backgroundcolor of UITableView to clearColor and set image like bellow..
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
///write your code..
UIImageView *img = [[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 45)] autorelease]; // set frame as your cell frame
img.image = [UIImage imageNamed:#"yourImageName"];
cell.backgroundView = img;
////write your code
}

For Sections you can code a delegate method like
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;//how many sections you want you can write a value after return type
}

Best way I found to make the corners right angled is to set the background view to nil and then put a view in it.
[cell setBackgroundView:nil];
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

Related

Creating a smaller UIPickerView for use in table

I'm trying to create a smaller UIPickerView for use in a table and I can't work out how to make the actual control resize correctly.
What I'm trying to achieve is something like the following:
I am creating my picker in the cellForRowAtIndexPath: method:
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.contentView.clipsToBounds = YES;
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:
CGRectMake(0, 0, cell.frame.size.width, 85)];
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
[myPickerView selectRow:3 inComponent:0 animated:YES];
[cell.contentView addSubview:myPickerView];
No matter what I set the height to, I get the same size UIPickerView. The first image is with 85 for the height. The second image is with 200 for the height.
The one thing I can do is change the cell height, but if I do this and set the height of the UIPickerView to the same value as the cell height, the picker isn't centred in the cell and the actual touch control point is near the bottom of the cell. The final image in this post illustrates this...
Result of "CGRectMake(0, 0, cell.frame.size.width, 85)"
Result of "CGRectMake(0, 0, cell.frame.size.width, 200)"
Picker Frame and Cell Height set to 100
Try to shrink the "height" of the UIPickerView
myPickerView.transform = CGAffineTransformMakeScale(0.7, 0.7);

How to add image on UITableView cells?

How can I add one image on several cells of UITableView?
I add an image to one top cell, like this:
But after scroll to bottom and top I see this:
Second and third cell has a z-index more that first cell.
How I can do this?
Thanks..
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imgView.image=[UIImage imageNamed:#"img.png"];
[cell addSubview:imv];
neither you can directly add image to cell
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* CellIdentifier = #"Cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
cell = [[[UITableViewCell alloc] UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = #"I'm a UITableViewCell!";
cell.imageView.image = [UIImage imageNamed:#"image.png"];
return cell;
}
try this code....
and adjust the image size....
Are you adding the image to the CellView or to the TableView?
You can try the following function in which you have to specify the filename, position of the picture, size and view to which you want to add it.
- (void)createImage:(NSString *)name
inX:(NSInteger)x
inY:(NSInteger)y
width:(NSInteger)w
height:(NSInteger)h
view:(UIView *)v{
UIImage *background = [UIImage imageNamed:name];
CGRect rect = CGRectMake(x, y, w, h);
UIImageView *yourImageView = [[UIImageView alloc] initWithFrame:rect];
yourImageView.image = background;
[v addSubview:yourImageView];
}
if you want to add the image on the tableView, get the scroll view instance from the tableview using tableviewobj.scrollview and then add the image as subview on the scrollview and make the image come front using bringsubviewtofront method.
Adjust your cell height as a image size.
If you want to add image on cell then I would recommend you to make custom cell with imageView .
You can add image as subview to table
UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imgView.image=[UIImage imageNamed:#"img.png"];
[UITableview addSubview:imv];
or
you can increase the height of cell
I did it.
Add UIImageView (let's say img1) object to UITableView (table1)
Set img1.Layer.ZPosition to 1 that the image was above cells in the table1
For Headers in sections of table set img1.Layer.ZPosition to 2 that a header was above an image
Thanks all for help!
You wouldn't add it to a single cell, but to the tableview itself. Then - in a tableview subclass - you would make sure that in the layoutSubviews the image view stays on top and the position gets adjusted to stay in a fixed place. Or next to a specific cell if you want that.
Use addSubview on tableview directly.
let imageView = UIImageView(frame: CGRectMake(0, 0, width, height))
imageView.image = UIImage(named: "My Image")
self.tableView.addSubview(imageView)

Is there a way to customize the size of the UITableViewCell's backgroundView?

I'm trying to assign a custom backgroundView to a UITableViewCell. The table is Grouped.
I'm a bit of a trailblazer so instead of using the typical width of a background view, 302 pixels, I'm trying to use a 300 px wide image.
Here's my code:
- (void)tableView:(UITableView*)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
// a 300 pixel wide image
UIImage *backgroundImage = [UIImage imageNamed:#"tableViewCellBackgroundTop"];
UIImageView *theBackgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
theBackgroundView.frame = CGRectMake(10, 0, 300, 49);
cell.backgroundView = theBackgroundView;
}
If I run this in the simulator and measure with xScope, the cell still appears to be 302 pixels wide. What gives? Is there some other property I need to set to make the framework respect the frame I've assigned to the background view, or were table view cells not intended to be customized in this fashion?
You can create a UIView instance and make theBackgroundView a subview of it, and then set cell.backgroundView to the created view:
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 49)];
[view addSubview:theBackgroundView];
cell.backgroundView = view;

UIScrollView and UIPageControl within UITableView

I've seen lots of sources saying it is possible to include a UIScrollView with UIPageControl inside a UITableViewCell to be able to scroll horizontally (through a list of selectable images), but can't find any raw examples that does what I want. I've gotten my scroll view "working", but I am unsure how to go forward - hopefully someone can send me some guidance.
Within my cellForRowAtIndexPath, I create a cell, and add both a scrollView and pageControl as subviews.
UITableViewCell *cell = [self.challengeListView dequeueReusableCellWithIdentifier:SubmitChallengeCellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SubmitChallengeCellIdentifier] autorelease];
cell.frame = CGRectMake(0, 0, 1000, 50);
}
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 50)];
[scrollView setContentSize:CGSizeMake(1000, 50)];
[[cell contentView] addSubview:scrollView];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 50, tv.frame.size.width, 50)];
[pageControl setNumberOfPages:4];
[[cell contentView] addSubview:pageControl];
I've attached a screenshot of what's being displayed
the bottom portion of the main view is my UITableView that contains the scrollView/pageControl (and it will scroll horizontally, as I can see the scrollerIndicator showing this), and I've got its method's set to the following:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
My Scroll view will indicate a "horizontalScroller" and I am able to scroll back and forth, but obviously there's no content there. How do I go about populating the tableView/scrollView with say, a list of "clickable" images? Any direction would be greatly appreciated - preferably not a "hey this guy's done it somewhat similar, check out this link" but maybe more an explanation of how this functionality should be implemented correctly (ESPECIALLY in regards to iOS 3.0+ - it is my understanding Apple has made our lives easier in implementing this)
I've solved my own problem; maybe the reason no once answered me is because its a minor implementation once you understand each view's purpose.
From within cellForRowAtIndexPath:
I created a standard UITableViewCell, however I altered the frame of the cell to my own custom frame of 1000 width by 50 height (catered to my needs for project).
I then created a UIScrollView, set it to the following (keep in mind I have my tableView defined in IB, so I'm mapping some of my height/widths to those values):
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 78)];
I then create the desired image view (I realize I will next create a loop that does many images and lays them out across the scroll view):
UIImage *image = [UIImage imageNamed:#"dummy.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 80, 78);
[scrollView addSubview: imageView];
Here's the part I was missing. After adding the scrollView to the cell contents, you need to use the UIPageControl (which didn't seem obvious to me for this implementation at first) to setup the actual "visual horizonal scrolling" affect:
[[cell contentView] addSubview:scrollView];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 50, tv.frame.size.width, 50)];
[pageControl setNumberOfPages:4];
[[cell contentView] addSubview:pageControl];
Hope that helps someone's search - I spent quite some time on Google looking for the example I just explained and didn't have much luck other than the general overview of how it would work.

Customize UITableView section border color for each section

I am using a UITableView with grouping. My first group I have built a custom cell with no background color, and I would also like to remove the border around the cell. How can I do this just for one section?
For my first cell, I would like to set the border/seperator style to [UIColor clearColor].
EDIT: Apple does this in their contacts app, and I wanted to design something similar.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0){
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}
}
I believe what Apple uses for its contact heading is tableView:viewForHeaderInSection: for section 0, instead of a cell in the first section. You can still specify a transparent background and the pinstripe will show behind it. The border will not be there as grouped table view section headers do not have borders.
Whether you built your custom cell in Interface Builder or using code, it should be a fairly trivial task to migrate those views from a UITableViewCell to a UIView for use with tableView:viewForHeaderInSection:.
I don't know if you'll be able to keep the Details title for, say, section 1, unless you make a label containing that word and add it to the section 0 header view manually.
If i understood your question, in the cellForRawIndexPath you should add-
if(indexPath.section==0){
//set the style you wish to add only to the first section
}
good luck
EDIT
I use this function for that -
+(void)setTransparentBgToCell:(UITableViewCell*)cell{
UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
backgroundView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backgroundView;
[backgroundView release];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
shani