Add UiStepper programmatically inside cells - objective-c

how do I add a UIStepper to every cell of my UITableView programmatically?
Thanks
Luca

One way to do it is to add the UIStepper to each cell as a subview in tableView:cellForRowIndexAtPath in your TableViewController. For example:
- (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];
UIStepper* stepper = [[UIStepper alloc] init];
stepper.frame = CGRectMake(220, 10, 100, 10);
[cell.contentView addSubview: stepper];
}
return cell;
}
This will add a stepper to the right-hand side of each cell in your table.

Related

NSTableViewCell.ImageView Setting Position?

I have a NSTableViewCell which has a ImageView within the cell.
What is needed to control the position of the image within the cell using code?
If you are using subclass of NSTextFieldCell(ImageAndTextCell). you can control positon of image in
- (NSRect)imageRectForBounds:(NSRect)cellFrame method.
You can create custom cell...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ImageOnRightCell";
UIImageView *photo;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
photo = [[[UIImageView alloc] initWithFrame:CGRectMake(225.0, 0.0, 80.0, 45.0)]];
photo.tag = PHOTO_TAG;
photo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:photo];
} else {
photo = (UIImageView *)[cell.contentView viewWithTag:PHOTO_TAG];
}
return cell;
}
This will look like it....
or u can read this link....
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html
instead of default TableViewCell, create your own custom tableViewCell and position it according to your need.

uiwebview in every tableviewcells

I need to display each webview in each of the tableviewcell in my Uitableview.When using the below code,when there are 2 elements,the first cell is empty but second cell is correct.hrs and hrsHtml contains all values,the problem is only the last data is displaying in their appropriate cell in tableview.Other cells are just blank.Also is total cells is 2,first we can only been able to see the 2nd cell only,but after scrolling tableview reloads and 2nd cell disappears and 1st cell gets displayed.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [brId count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[ UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.contentView addSubview:hrs];
hrsHtml = [NSString stringWithFormat:#" <font size=\"2\" face=\"Arial\">%# </font>",[html objectAtIndex:indexPath.row]];
[hrs loadHTMLString:hrsHtml baseURL:nil];
return cell;
}
Screenshot when tableview appears,Only webview in cell 2
Screenshot when tableview scrolls,Only webview in cell 1,cell 2 disappears
Since hrs and hrsHtml are single objects, you only have one of each even though you have multiple cells. If you modify hrs, it will change for all cells since they seem to be sharing it. (Unless you have other code somewhere that changes the objects that those variables point to.)
Another odd thing is that you use a brId array to determine the number of rows and a html array to get the row content. If those ever get out of synchronization, you will have problems.
Also, you should only add a subview to a cell when you create a new one.
- (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:nil] ;
}
hrs = [[UIWebView alloc] initWithFrame:CGRectMake(10,0,320,84)];
hrs.userInteractionEnabled = YES;
hrs.backgroundColor = [UIColor clearColor];
hrsHtml = [NSString stringWithFormat:#" <font size=\"2\" face=\"Arial\">%# </font>",[html objectAtIndex:indexPath.row]];
[hrs loadHTMLString:hrsHtml baseURL:nil];
[cell.contentView addSubview:hrs];
hrsHtml = nil;
return cell;
}
Now the webview loads correctly in every tableviewcell.
i think you have problem in initializing cell try below code
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}

subtitle alignment in UITableViewCell

i`m trying to build a table view with subtitle text in cells
the problem is when i try to set the alignment of the subtitle text to the right it doesn't work, but it works fine with the main text
here is my code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
[[cell textLabel] setTextAlignment:UITextAlignmentRight];
[[cell detailTextLabel] setTextAlignment:UITextAlignmentRight];
cell.textLabel.text = [array objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:18];
cell.detailTextLabel.text = #"test";
return cell;
}
when i remove the subtitle code the alignment works fine
any idea ?
Ok so subclass UITableView Cell and customize the labels with init. You can override layoutSubviews and move the labels to the right:
- (void)layoutSubviews {
[super layoutSubviews];
self.textLabel.frame = CGRectMake(0.0, 68.0, 80.0, self.frame.size.height);
self.detailTextLabel.frame = CGRectMake(0.0, 68.0, 120.0, self.frame.size.height);
}
These are just example values so you can get the idea.
Why you are initializing cell twice:
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
You can create custom cell with two labels and provide alignment to those label.Follow these steps:
1.Add new file subclass of UITableViewCell say labelCustomCell.
2.In labelCustomCell create two labels say label1 and label2.
3.In initWithStyle method allocate these labels and provide alignment.
4.In layoutSubViews method assign frame to those labels.
5.In cellForRowAtIndexPath method write following code:
static NSString *CellIdentifier = #"DataEntryCell";
labelCustomCell *cell = (labelCustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[labelCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.label1.text = [array objectAtIndex:indexPath.row];
cell.label1.font = [UIFont systemFontOfSize:18];
cell.label2.text = #"test";
Do not forget to import labelCustomCell.

Table cell subtitles not displaying

I setup the cell style to subtile in the inspector but still no subtitles, just blank but the title is displayed
here is the section in the view controller:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"homeworkcell"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"homeworkcell"];
}
cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [tablesubtitles objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
//static NSString *CellIdentifier = #"Cell";
/* if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}*/
// Configure the cell.
//-----------------------------------------START----------------------------Set image of cell----
cellImage = [UIImage imageNamed:#"checkboxblank.png"];
cell.imageView.image = cellImage;
//--------------------------------------------END---------------------------end set image of cell--
return cell;
}
You need to use the UITableViewCellStyleSubtitle when you create a new UITableViewCell, not UITableViewCellStyleDefault. UITableViewCellStyleDefault only shows the textLabel in the cell, not the detailTextLabel.
See the "Cell Styles" section of the UITableViewCell reference and the "Standard Styles for Table View Cells" of the Table View Programming Guide.

adding different UILabel to each cell in UITableView

I'm adding different text to each cell in UITableView. However when I do that, test is displayed in every cell except for the first one..So if theres an array with 9 numbers between 1 to 9, 1 is displayed in the second cell, 2 is displayed in the third cell and respectively. There's nothing shown in the first cell fromHeres the codes
// Customize the appearance of table view cells.
- (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];
cell = [self getCellContentView:CellIdentifier];
}
if (indexPath.row == 0) {
NSLog(#"hi");
}
//add another textfield
NSString *path = [[NSBundle mainBundle] pathForResource:#"Rankvalue" ofType:#"plist"];
NSMutableArray* rank = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSString *rankValue = [rank objectAtIndex:indexPath.row];
UILabel *rankLabel = (UILabel *)[cell viewWithTag:1];
rankLabel.text = rankValue;
[rankLabel setFont:[UIFont fontWithName:#"austin power" size:[#"40" intValue]]];
CGRect labelFrame = CGRectMake(220,70,50,40.0);
[rankLabel setFrame:labelFrame];
// Configure the cell(thumbnail).
cell.textLabel.text = [self.stars objectAtIndex:indexPath.row];
NSString *path2 = [[NSBundle mainBundle] pathForResource:#"Filename" ofType:#"plist"];
self.filename = [[NSMutableArray alloc] initWithContentsOfFile:path2];
cell.imageView.image = [UIImage imageNamed:[self.filename objectAtIndex:indexPath.row]];
//transparent cell
cell.backgroundColor = [UIColor clearColor];
[rankLabel release];
[rankValue release];
return cell;
}
And this is the code for the subview of the cell
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
CGRect CellFrame = CGRectMake(0, 0, 320, 65);
CGRect Label1Frame = CGRectMake(17,5,250,18);
UILabel *lblTemp;
UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:cellIdentifier] autorelease];
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
[lblTemp setFont:[UIFont fontWithName:#"Arial-Bold" size:15]];
lblTemp.tag = 1;
lblTemp.backgroundColor=[UIColor clearColor];
lblTemp.numberOfLines=0;
[cell.contentView addSubview:lblTemp];
return cell;
}
initWithFrame:reuseIdentifier: has been deprecated for initWithStyle:reuseIdentifier:
You should create one of the standard styles.
Set the values you want (font, etc.) on cell creation, not on refresh. Frame of text field is the same. And background color.
You really don't want to reload that plist (oh, there's two of them!) every time you refresh a cell! Load it once in another method and cache it as an ivar.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// create and set up all the layout attributes of the cell
// it should be auto released
// which should include a call like the following, or loading a cell from a nib
// cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
// reuseIdentifier:CellIdentifier];
// [cell autorelease];
}
// the only thing that should happen here is setting the data values
// into the cell!!!!!! All these values should be loaded once and
// kept as "model state" by the controller. Do not create an image
// each time through, do not load an entire plist to access one item
// each time through
rankLabel.text = rankValue;
cell.textLabel.text = [self.stars objectAtIndex:indexPath.row];
cell.imageView.image = [self imageAtRow:indexPath.row];
}
If the image your displaying is different for each cell, using imageNamed: is not appropriate. If there's 2 or 3 images that indicate type of cell that will each be used a lot, imageNamed: is likely preferable. imageWithContentsOfFile: is your other option, you'll need to build the full path
Try switching this:
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
//cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell = [self getCellContentView:CellIdentifier];
}
for this:
UITableViewCell *cell=[self getCellContentView:CellIdentifier];
Not saying it will work, but just give it a shot. Also, do this inside your getCellContentView:
[lblTemp release]
Or you will have a leak.