AutoResizing Issue - objective-c

I added a UIImageview as a subview of UITableViewCell.contentview. It has margins in left and right side of the cell. when rotating device, Autoresizing is not working for me. I am using:
UIImageView *imgviewcellBg=[[UIImageView alloc]initWithFrame:CGRectMake(9, 3, self.view.bounds.size.width-18, 33)];
imgviewcellBg.autoresizesSubviews=UIViewAutoresizingFlexibleWidth;
[cell.contentView addSubview:imgviewcellBg];
Help me. Thanks.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier]autorelease];
cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, tblMyWines.frame.size.width, cell.frame.size.height);
}
UIImageView *imgviewcellBg=[[UIImageView alloc]initWithFrame:CGRectMake(tbl.frame.origin.x+5, 3, tbl.frame.size.width-10.0, 33)];
[cell.contentView addSubview:imgviewcellBg];
return cell;
}

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.

Add background image to all UITableViewCell

I used willDisplayCell delegate method to show a custom background image to a UITableViewCell.
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
static UIImage* bgImage = nil;
if (bgImage == nil) {
bgImage = [UIImage imageNamed:#"myImage.png"];
}
cell.backgroundView = [[UIImageView alloc] initWithImage:bgImage];
}
When I run the app in the simulator, the background only for the cells that are drawn are changed, is there a way to change all cells' background?
I have worked this code.It's working good .you can try this code.
- (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 = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"Cell"] autorelease];
UIImageView *cellBackView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 100)];
cellBackView.backgroundColor=[UIColor clearColor];
cellBackView.image = [UIImage imageNamed:#"list.png"];
cell.backgroundView = cellBackView;
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}
You have to set the backgroundView property in the cellForRowAtIndexPath method that creates the cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.backgroundView = [[UIImageView alloc] initWithImage:...];
return cell;
}

UITextView in a grouped UITableView's cell

Please, explain me: how to programmatically create an UITextView in UITableViewCell (UITableView has a grouped style). The text view size should be equal to size of the cell.
The UITextView has a gap between its text and its border (top left corner), so I need the correct coordinates to properly place the text view on a cell.
UPDATE: I've solved my question. See my self-answer below. Thanks!
It was simple:
textView.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height);
Here you go:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Prototype Cell"];
UITextField *textfield;
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"Prototype Cell"];
textfield = [[UITextField alloc] init];
[cell.contentView addSubview:textfield];
textfield.tag = TEXT_FIELD_TAG; //Suppose you defined TEXT_FIELD_TAG someplace else
}
else
{
textfield = [self.view viewWithTag: TEXT_FIELD_TAG];
textfield.frame = cell.frame;
}
return cell;
}
Set "contentInset" of UITextView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *sCellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:sCellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDetail
reuseIdentifier:sCellIdentifier];
}
UITextView *textView = [UITextView alloc]init];
textView.contentInset = UIEdgeInsetsMake(-8,-8,-8,-8); //Change according ur requirement
textView.frame = cell.frame;
[textView setUserInteractionEnabled:FALSE];
[textView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubView:textView];
return cell;
}

Adding a UIPickerView to a UITableView cell

I am trying to add a pickerView to a row in my table view, but after doing this, all I see is a black rectangle inside the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
pickerView = [[[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 200, 300)] autorelease];
[cell.contentView addSubview:pickerView];
}
}
I really don't have a clue how to solve this.
Can anyone help me, please?
May be this can help: DateCell with TableViewController

where can find a example on adding UIButton in each UITableCell

Where can find a example on adding UIButton in each UITableCell?
You need to create a UIbutton and jus addSubView to the cell it will work fine.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
UIImageView * accerTypeImage;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIButton *button = [[UIButton allo] init];
button.frame = //give the frame in UITableViewCell.
button.tag = indexPath.row;
[cell addSubView:button];
[button release];
return cell;
}