NSTableViewCell.ImageView Setting Position? - objective-c

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.

Related

How can I display a thumbnail image to Table Cell in xCode?

As a Xcode beginner, I want to display a thumbnail in the table cell in my app. For now, I have this code which parses JSON data and posts it in the title and subtitle of the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MainCell"];
}
cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:#"receta"];
cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:#"koha"];
return cell;
}
How can I show a thumbnail in the right of the cell?
Thanks.
If you don't want to make a custom cell, you can create a UIImageView with your desired image and set it as the cell's accessoryView, which will show it on the right edge of the cell. You also need to ensure that the height of the cell is tall enough to fit the image's height.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
- (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];
}
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"some-image-name"]];
return cell;
}
You can return a constant (I chose 100) if the image will always be a fixed size, or you can check the UIImage's size and return something like size.height + 10 for some extra padding.

When I used layoutSubviews method of UItableViewcell with category in my file,something disappeared

when I used layoutSubviews method of UItableViewcell with category, just like the code below
#implementation UITableViewCell (forimage)
- (void)layoutSubviews{
[super layoutSubviews];
self.imageView.frame = CGRectMake(0, 0, 50, 50);
}
#end
when I used the code below to draw the cells , the textLabel was disappeared ,anyone know why it be that~, and does that mean if I use layoutSubviews,I must write all the subviews what I need in the method?
- (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];
}
RadioInfo *radioinfo = [radios objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%#",radioinfo._name];
if(!radioinfo._logo){
if(self.tableView.dragging == NO && self.tableView.decelerating == NO){
[self startPicDownload:radioinfo forIndexPath:indexPath];
}
cell.imageView.image = [UIImage imageNamed:#"1.jpg"];
}
else {
cell.imageView.image = radioinfo._logo;
}
return cell;
}
What you want to do is add to the behaviour of UITableViewCell's layoutSubviews method, not replace it.
To properly add to the behaviour, subclass the cell and perform your own layout, as you have above but add a [super layoutSubviews] right at the top of your method to ensure that the cell's own basic layout is performed first.

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

How to avoid the repaint effect when using the UITableView?

I was debugging an issue with a view controller recently and noticed that each time I drag the view up or down it will repaint the entire contents of my UITableView (as it calls the cellForRowAtIndexPath method each time). Is it possible to use an in memory datasource or add another delegate to my view controller so it won't repaint each time?
I'm not modifying anything inside the cells when the user interacts with it so my data source would be static after the initial "viewDidLoad" is called.
- (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];
}
if ([self.hats count] > 0) {
//do some complex stuff in here that hurts each time we re-draw this ...
}
return cell;
}
Thank you in advance
so // complex stuff means adding UIViews.
I make an example for an UIImageView. Since you don't show the complex stuff you have to adopt it on your own.
your code looks like this:
- (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];
}
if ([self.hats count] > 0) {
UIImageView *imageView = [[UIImageView....];
[cell.contentView addSubView:imageView];
[imageView setImage:foo];
[imageView release];
}
return cell;
}
refactor your code that it looks like this:
- (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];
UIImageView *imageView = [[UIImageView....];
[cell.contentView addSubView:imageView];
imageView.tag = 42;
[imageView release];
}
if ([self.hats count] > 0) {
UIImageView *imageView = [cell viewWithTag:42];
[imageView setImage:foo];
}
return cell;
}
et voila, your tableview is responsive. Because you create the subviews exactly one time for each cell. And when the cell isn't used anymore and goes into the reuse bin the subviews stay with it.
And if you need 4 imageViews in one cell and 8 in another, you add 8 imageviews when you create the cell and give them a frame of CGRectZero and of course a different tag for each view.
If you need them you show them, if you don't need them you set the image to nil and the frame to zero.

UITableViewCell backgroundView image resizing?

I am using an image to display the whole content of a UITableViewCell and it's set using the backgroundView property.
My problem is that it is always scaled to fit the cell. Is there a way to disable scaling in this case, so I can provide the 480 pixel version only and it just gets cropped when orientation is portrait?
I'm doing it like this:
- (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];
}
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"menu.0%d.large.png", indexPath.row+1]]];
UIImageView *selectedBgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"menu.0%d.large.selected.png", indexPath.row+1]]];
cell.backgroundView = bgView;
cell.selectedBackgroundView = selectedBgView;
[bgView release];
[selectedBgView release];
return cell;
}
I tried using two versions of the images to switch between them when orientation is changed. This works so far, too, but it's got the drawback that you always see the scaling while the animation is processed.
Thanks for your help
Arne
Set the content mode of your background image views.
bgView.contentMode = UIViewContentModeTopLeft;
selectedBgView.contentMode = UIViewContentModeTopLeft;
This tells the UIImageView to put its image in the top left corner rather than scale it to fit.
I did not try this on the simulator or device, but the following might work:
- (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];
}
// New!
cell.backgroundView.clipsToBounds = YES;
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"menu.0%d.large.png", indexPath.row+1]]];
UIImageView *selectedBgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"menu.0%d.large.selected.png", indexPath.row+1]]];
// New!
bgView.center = CGPointMake(160, 25); // Adjust the y-Value to be exactly half of the height of your cell
bgView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin; // Not quite sure whether these two will work, maybe you have to play a little with the Masks
cell.backgroundView = bgView;
cell.selectedBackgroundView = selectedBgView;
[bgView release];
[selectedBgView release];
return cell;
}
Let me know whether you managed to get it running.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
is was you looking for, I think.
more information :
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html