How to create these kind of badges? - objective-c

I sketched this prototype: http://dl.dropbox.com/u/3630641/3-%20Todolist%20Main.jpg
The number at the left is the item priority and list is re-orderable so the values will change. How to create these left-located badges? I prefer a code approach if possible, not PNG images.
Update:
Would you please have a look:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
CGRect priorityLabelRect = CGRectMake(10, 5, 20, 30);
UILabel *priorityLabel = [[UILabel alloc] initWithFrame:priorityLabelRect];
priorityLabel.layer.cornerRadius = 5.0f;
priorityLabel.layer.backgroundColor = [[UIColor grayColor] CGColor];
priorityLabel.tag = kPriorityValueTag;
[cell.contentView addSubview:priorityLabel];
[priorityLabel release];
CGRect meatLabelRect = CGRectMake(80, 5, 300, 30);
UILabel *meatLabel = [[UILabel alloc] initWithFrame:meatLabelRect];
meatLabel.tag = kMeatValueTag;
[cell.contentView addSubview:meatLabel];
[meatLabel release];
cell.showsReorderControl = YES;
}
// Configure the cell.
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
But No grey area is shown around the priority.
If I add a new one, a grey area appears for less than a second around the priority but it disappears immediately. Not to mention that the priority value isn't located at the center for the grey area.
Any idea what I'm doing wrong?

Add a UILabel to the cell and format it to your liking. For the rounded corners, set label.layer.cornerRadius.

Create a UIView for the badge.
look here it will explaine how to draw what ever you want the badge to look like. and draw the number :
http://www.techotopia.com/index.php/An_iPhone_Graphics_Drawing_Tutorial_using_Quartz_2D
create a variable that will receive the cell number and draw it in the cell.
It should look something like that -
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGRect rectangle = CGRectMake(10,10,40,40);
CGContextAddRect(context, rectangle);
CGContextStrokePath(context);
CGContextSetFillColorWithColor(context, [UIColor grayColor].CGColor);
CGContextFillRect(context, rectangle);
CGPoint point =CGPointMake(20,20);
[self.cellNumber drawAtPoint:point withFont:font];
}
4 add the BadgeView to your cell and in the CellForRawAtIndexPath pass the number to the badge view.
good luck

You could make a combination control. It's a UIImageView with a UIImage and a UILabel as subviews.
A regular object that has a UIImageView.image with the gray rectangle and then a UILabel for the white number. Then just change the value of the label's text.
If you start with a UIImageView and subclass that, you will be able to just stick it directly into the regular UITableViewCells since they already have a space for a UIImageView.

Related

Unable to create bottom border on UICollectionView Cell

I need to add a 1px bottom border to my UICollectionView cells but I can't get it working, I tried the following code but the border doesn't show:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
cell.backgroundColor = [UIColor whiteColor];
cell.titolo.text = arrayt[prova];
//create the border
UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height, cell.frame.size.width, 1)];
bottomBorder.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:bottomBorder];
return cell;
}
What could be the problem?
CGRectMake gets (x,y,width,height) as parameter so I can't understand what's wrong on my code
You need to have correct y offset
//create the border
UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - 1, cell.frame.size.width, 1)];
bottomBorder.backgroundColor = [UIColor redColor];
You should set the frame of the cell to a known (not zero size) value before adding subviews like this. Then, after you have created your view with the correct frame you need to set the autoresizing mask to ensure that the view remains at the bottom of the view (flexible top, flexible width).
This is not the way to solve this, UIInsets are better way, as shown in this post: https://stackoverflow.com/a/22823146/12652 . But if you still want to follow this way, you need to remove these views afterwards or they will be kept adding to UICollectionViewCell. Considering you also have UIImageView as a childview :
UIView *bottomBorder = [[UIView alloc] initWithFrame:CGRectMake(0, cell.frame.size.height - 1, cell.frame.size.width, 1)];
bottomBorder.backgroundColor = [UIColor colorWithRed:162/255.0f green:143/255.0f blue:143/255.0f alpha:1.0f];
for(UIView *view in cell.contentView.subviews){
if([view isKindOfClass:NSClassFromString(#"UIImageView")])
continue;
else{
[view removeFromSuperview];
}
}
[cell.contentView addSubview:bottomBorder];

How to make UITableView cell separator like iOS7?

In iOS 7 , default UITableViewCell separator is a little bit cutting from left that can show cell's imageView.
I can change to full size with following codes.
[self.tableViewMain setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
And i want to change it when my iPad orientation is Portrait.
So i tried following codes in rotate event
if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation) ||(UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)))
{
[self.tableViewMain setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
}
else
{
[self.tableViewMain setSeparatorInset:UIEdgeInsetsMake(15, 0, 0, 0)];
}
I want to do it like iOS7 Cell Separator manually.
How can i do it?
You mean by manually Interface Builder ? if it's the case, click your UITableViewCell in IB and go to the third panel, in Separator insets choose custom and you can put the left and right value.
Edit : if you want to do it by code, see the documentation of UIEdgeInsets structure :
UIEdgeInsets
Defines inset distances for views.
typedef struct {
CGFloat top, left, bottom, right;
} UIEdgeInsets;
Then for example :
self.tableViewMain.separatorInset = UIEdgeInsetsMake (0, 15, 0,0);
Here you go.
[self.tableViewMain setSeparatorInset:UIEdgeInsetsMake(15, 0, 0, 0)];
EDIT:
Above answer is wrong. Adding a custom UIView will solve this.
Your cellForRowAtIndexPath should be lie this
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
UIView *lineView;
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
lineView = [[UIView alloc] initWithFrame:CGRectMake(15, cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1)];
[cell.contentView setFrame:CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height-1)];
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
lineView.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview:lineView];
[lineView release]; // IGNORE IF USING ARC
}
[cell.textLabel setText:#"YOUR_TEXT"];
return cell;
}
You should negative value in the place of left inset
self.tableView.separatorInset = UIEdgeInsetsMake (0, -15, 0,0);

add project with app delegate

I'm trying to add a dynamic horizontal scroll to a table cell.
I found an example after searching for a while, but since the example project only was the view it was directly connected to the app delegate which contained the majority of the relevant code. Most of it within applicationDidFinishLaunching.
Do any of you know how I am supposed to add this code to my project?
Thanks in advance, Tom
EDIT:
Here's the link to the sample project i downloaded: http://blog.sallarp.com/wp-content/uploads/2008/11/slidemenu.zip
Here's the youtube clip of how it's supposed to work: http://www.youtube.com/watch?v=wFqBNXZ4SHI
EDIT 2 (New code):
"famorables" is declared with 5 one-word-strings
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyFamorablesCell";
UITableViewCell *cell;
//Create Cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//Create a UIScroll View
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 80)];
scrollview.contentSize = CGSizeMake(self.view.frame.size.width * 2, 80);
scrollview.showsHorizontalScrollIndicator = NO;
float totalButtonWidth = 0.0f;
famorableArray = self.famorables;
for(int i = 0; i < [self.famorables count]; i++){
UIButton *famorableButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[famorableButton setFrame:CGRectMake(0.0f, 0.0f, 100.0f, 30.0f)];
[famorableButton setTitle:[NSString stringWithFormat:#"%#", [famorableArray objectAtIndex:[indexPath row]]] forState:UIControlStateNormal];
// Move the buttons position in the x-demension (horizontal).
CGRect btnRect = famorableButton.frame;
btnRect.origin.x = totalButtonWidth;
[famorableButton setFrame:btnRect];
// Add the button to the scrollview
[scrollview addSubview:famorableButton];
// Add the width of the button to the total width.
totalButtonWidth += famorableButton.frame.size.width;
}
[cell.contentView addSubview:scrollview];
return cell;
}
Okay Try this
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
//Create Cell
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//Create a UIScroll View
UIScrollView scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, (it will be the height of the Cell u want))];
scrollview.contentSize = CGSizeMake(self.view.frame.size.width * 2,(it will be the height of the Cell u want));
scrollview.showsHorizontalScrollIndicator = NO;
/*
Now add every this u might want to add in ur Scroll view.
keep that in mind that the height and width of contentSize and ScrollView will be depandent on ur requirement
Make sure u add every thing u want in the Scroll view will be the part of Scroll View
like for label
//Add Something to Scroll View
[scrollView addSubview:label];
*/
//Add Scroll view to Cell
[cell.contentView addSubview:scrollView];
return cell;
}
This will Work
In case of any queries feel free to ask

IOS UITableViewCell Spacer (or margin)

I'm trying to get a space between my table custom view cells on my table view controller. For example, for each cell created it is stacked up one by one and there is no margin. For example on the facebook iPhone app has a spacer after each cell which i would like to create, any ideas guys?
Edit: From comment below
The code looks like this
MSRSalesCompanyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
cell.backgroundView = [[CustomCellBackground alloc] init];
NSDictionary *company = [companies objectAtIndex:[indexPath row]];
cell.companyName.text = [company objectForKey:#"Name"];
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView.bounds = CGRectMake(0, 0, 320, 55);
[self loadImageAsync:cell withImageUrl:imageURL];
Check my answer for the similar question, it suggests you to create the invisible cells between the cells you want to divide with some space.
Option 2 is to create only the visible cells, make their height above the visible area and prepare special content and selected view's, note the selected view creation is not so easy and you'll need to do it as you probably don't want the spacing area to get selected, so i'm using the first option when there's a need to get some cell's spacing.
The main (and probably the only) disadvantage of the first option is that you have to treat the cell's indexes in a special way to distinguish the spacing-cells and the visible-cells.
If your tableView's rowHeight is 50 points, make your background image view height say, 40 points, and centre it in the cell. Then make your cell background colour [UIcolor clearColor]
Okayy i did some thing like this.....
First the BGColor Of view which will hold the Tableview is set to White (it was a personal choice w.r.t to my design) and then the cellForRowAtIndexPath method looks some thing like this.
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell;
cell = nil;
//Create Cell
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
//Retrive Values From DB
DBTable_Description *dbObject = [[DBTable_Description alloc]init];
dbObject = [contentList objectAtIndex:[indexPath row]];
//Cell View
UIView *cellView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 65)];
//ImageView
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(4.0, 8.0, 300.0, 58.0)];
imageView.userInteractionEnabled = NO;
imageView.image = imgForCellBkg;
//Label
UILabel *lblFor = [[UILabel alloc]initWithFrame:CGRectMake(70, 25, 200, 21)];
lblFor.text =dbObject.field1;
lblFor.backgroundColor = [UIColor clearColor];
lblFor.font = [UIFont fontWithName:#"Helvetica Neue" size:16];
lblFor.textColor = [UIColor grayColor];
lblFor.shadowColor = [UIColor grayColor];
//Adding Views to Cell View
[cellView addSubview:imageView];
[cellView addSubview:lblFor];
[cell.contentView addSubview:cellView];
cell.selectionStyle = NO;
return cell;
}
Okay First and formost neglect the Database code.
Now what i did is i created a View on each Cell name cellView (set the height as per ur requirement) next i had an Image View and set the appropriate image (again it may not be the thing u want to do) but pay attention to the CGRectMake i se the values according to the amount of gap i want b.w my cells.. the rest is usual.
here is the image of the view i had in my App
Let me Know if it worked
Cheers
W

Put a UIButton inside a UITableView

I am trying to put a UIButton inside a UITableViewCell.
The UITableView also has cells containing UILabels and and the cells have gradient backgrounds being drawn into them. The problem I am seeing is that my UIButton is just a filled black cell with no text display, and changing both the color and background color properties does not appear to do anything.
Here's the code I use to create the button:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"InAppViewController, doing this - width= %f", self.wdth);
static NSString *ProductTableIdentifier = #"ProductTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ProductTableIdentifier];
if (cell == nil)
{
if (self.wdth >= 700) {
CGRect cellFrame = CGRectMake(0, 0, (self.wdth - 100), 40);
cell = [[[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier: ProductTableIdentifier] autorelease];
// Set up some labels...
CGRect seenValueRect = CGRectMake(self.wdth-320, 0, 100, 40);
UIButton *seenValue = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[seenValue setBackgroundColor:[UIColor greenColor]];
seenValue.tag = kSeenValueTag;
seenValue.frame = seenValueRect;
[seenValue setTitle:#"I'm a clone" forState:UIControlStateNormal];
[cell.contentView addSubview:seenValue];
[seenValue release];
// Continue setting up cell...
The pastebin link contains the code to the entire cellForRowAtIndexPath() function (for the UITableView in question).
http://pastebin.com/bpEyfruE
Maybe the problem is that you're releasing the UIButton innitialized with convenience initializer [UIButton buttonWithType:(UIButtonType)]. Try removing the [button release]
It can't even be that you have to do [cell addSubview:seenValue]; instead of [cell.contentView addSubview:seenValue];
You could also try to do CGRectMake(110, 11, 185, 30) instead of CGRectMake(self.wdth-320, 0, 100, 40): I use it and it works well but I don't know if it what you need