I have an image size : CGRectMake(100,70,50,50) inside cellForRowAtIndexPath. I need to change the frame size image when the cells is odd
Code is:
if(indexPath.row %2)
{
CGRectMake(200,70,50,50)
}
else
{
CGRectMake(100,70,50,50)
}
In the first time when I load TableView, it's work ok, but when I scroll again and again, the frame size image display is in the wrong position. It changes very funny, I don't know why.
Can you help me
Thanks a lot.
You have set set the frame on your UIImageView subview:
if(indexPath.row %2) {
cell.yourImageView.frame = CGRectMake(200,70,50,50)
} else {
cell.yourImageView.frame = CGRectMake(100,70,50,50)
}
You can do it following way.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UIImageView * imageView;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if(indexpath.row%2 == 0)
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(200, 70, 50, 50)];
else
{
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 70, 50, 50)];
[cell.contentView addSubview:imageView];
imageView.tag=11;
[imageView release];
}
}
else
{
imageView = (UIImageView*)[cell.contentView viewWithTag:11];
}
// here are couple of other statements
...............
}
Personally, I would create a custom table cell and do something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ImageCell";
ImageCell *cell = (ImageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell configureWithRow:indexPath.row];
}
Then in your custom table cell, create an IBOutlet for your resizable image view and add a method:
-(void)configureWithRow:(NSInteger)row
{
if(indexPath.row %2)
{
self.resizableImageView.frame = CGRectMake(200,70,50,50)
}
else
{
cell.resizableImageView.frame = CGRectMake(100,70,50,50)
}
}
This does a couple of things
You don't have to check for a nil cell because you are guaranteed to get a cell back dequeueReusableCellWithIdentifier as long as the identifier matches one in your storyboard and your view controller is a UITableViewController.
You move the logic for the configuration of the cell to the custom table view cell, which is where it really should be. The view controller shouldn't care about the size of the image in the cell.
Related
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier= #"Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIImageView *typeImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 15, 15)];
[cell addSubview:typeImageView];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (indexPath.section == 0) {
}
else if (indexPath.section == 1) {
typeImageView.image = [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
}
return cell;
}
Here section 0 contain no imageview but when we scroll then image added in section 0. Everytime, when we scroll tableview images overridden to previous one.
This is a cell dequeue problem. Try to set nil in the section 0 image view.
Also you are adding a image view as subview for all cells. This will get the problem. When adding subview check whether subview already exists or not.
I hope this is useful.
I have a custom cell class called GameCell, the UI being created in storyboard. When my cells load, they load on top of each other. This problems occurs for cell.clipsToBounds YES and NO, just in different variations:
I have also tried [cell setClipsToBounds:(BOOL)] with no success.
Here's my cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"gameCell";
GameCell *cell = (GameCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell.contentView.superview setClipsToBounds:NO];
NSLog(#"made a cell");
PFObject *myPartners = [self.games objectAtIndex:indexPath.row];
PFUser *partner = [self.partnerList objectAtIndex:indexPath.row];
// Cell profile image
cell.profileImage.layer.cornerRadius = cell.profileImage.frame.size.width / 2;
cell.profileImage.clipsToBounds = YES;
if([partner objectForKey:#"profilePic"]!=nil){
cell.profileImage.image = [partner objectForKey:#"profilePic"];
}
else {
cell.profileImage.image = [UIImage imageNamed:#"smile_green.png"];
}
//Cell indicators
if((int)[myPartners objectForKey:#"sent"]==1){
cell.myIndicator.image = [UIImage imageNamed:#"arrow_icon_dbl.png"];
}
else if ([myPartners objectForKey:#"sent"]==0){
cell.myIndicator.image = [UIImage imageNamed:#"arrow_icon.png"];
}
cell.partnerName.text = [myPartners objectForKey:#"receiverName"];
cell.gameId = myPartners.objectId;
// Cell drop shadow
[cell.cellView.layer setShadowColor:[UIColor blackColor].CGColor];
[cell.cellView.layer setShadowOpacity:0.5];
[cell.cellView.layer setShadowRadius:2.0];
[cell.cellView.layer setShadowOffset:CGSizeMake(1.0, 1.0)];
// Cell buttons
if([myPartners objectForKey:#"picture"]!=nil) {
[cell.myPlay setEnabled:NO];
} else {
[cell.myPlay setEnabled:YES];
}
return cell;
}
The view is a UITableViewController embedded in a NavigationController
You have used dequeueReusableCellWithIdentifier:
use like this
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell %d",indexPath.row];
UITableViewCell *cell=[self.tableView cellForRowAtIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
and also check this link.
UITableView cell Contents are disappearing and overlapping when scrolled in UITableViewcell?
You should make sure that the height of your cell is the RowHeight of your UITableView control.
e.g.,
Cell is designed from XIB, and the height of the view is 30, then make sure:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 30.0f;
}
In attributes inspector cancel Adjust Scroll view insets option.
you need clicked view controller .
How to draw image on the right side of the UITableView cell on iOS 7 on iPad?
Tried and don't help the following code:
tableView.contentInset = UIEdgeInsetsZero;
[cell contentView].frame = CGRectMake(0, [cell contentView].frame.origin.y, cell.frame.size.width, [cell contentView].frame.size.height);
You most likely want to set the accessoryView property on UITableViewCell. This will put a an image view on the right side of a cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Configure cell
}
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"<some image name>.png"]];
cell.accessoryView = image;
}
I am having trouble removing the white border that appears between each cell inside my UITableView. I have tried using:
[myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
inside my viewDidLoad method, but unfortunately that does not seem to work. I am using a background image for my cells to display, and it is the border between these images that I wish to remove. The image itself is the size of each cell, and the size of each cell I set in my CustomTableViewCell class as follows:
self.contentView.frame = CGRectMake(0, 0, 320, 80);
My cellForRowAtIndexPath method is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"mycell";
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.ID.text = [[DataModel sharedInstance].ID objectAtIndex:indexPath.row];
cell.Name.text = [[DataModel sharedInstance].Name objectAtIndex:indexPath.row];
cell.Date.text = [[DataModel sharedInstance].Date objectAtIndex:indexPath.row];
[cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Cell.png"]]];
return cell;
}
Despite everything I have done, I am seeing a clear white border between my cells in the table. I really would like to remove this. Can anyone see what I am doing wrong?
Inside Interface Builder - change the separator to None and then the separator color to clear color:
Setting UITableView separatorColor at viewDidLoad might be too early. Try viewWillAppear instead.
I've subclassed the UITableViewCell to add custom appearance to it. At the init level of the MYTableViewCell I added 4 subviews: UIImageView, and three UILabel(s). All 4 subviews have a different Tag assigned to them.
Inside the cellForRowAtIndexPath method I either create a new cell if it wasn't available at first, or reuse available one and assign the proper text to the ui labels.
The problem I am having is that if I try to scroll super fast, then the data gets messed up, however if I scroll up and down more slowly, then everything works fine.
Any thoughts??
Below is the code:
- (MyTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"itemListTableViewCell";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
DisplayableEntity *displayableEntity = [self.fetchedResultsController objectAtIndexPath:indexPath];
if( ! cell ) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[self tableView:tableView appearanceForCell:cell withEntity:displayableEntity];
} else {
UIImageView *imageView = (UIImageView *) [cell viewWithTag:IMAGEVIEW_TAG];
imageView.image = [UIImage imageNamed:displayableEntity.displayImageName];
UILabel *titleLabel = (UILabel *) [cell viewWithTag:TITLEVIEW_TAG];
titleLabel.text = displayableEntity.entityName;
UILabel *itemDescription = (UILabel *) [cell viewWithTag:DESCRIPTION_TAG];
itemDescription.text = displayableEntity.entityDesctiption;
}
}
// some code removed to make it brief
- (void)tableView:(UITableView *)tableView appearanceForCell:(MyTableViewCell *)cell withEntity:(DisplayableEntity *)entity {
// cell image view
UIImageView *cellImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[entity displayImageName]]];
[cellImageView setTag:IMAGEVIEW_TAG];
[cell addSubview:cellImageView];
// adding entity name label
UILabel *itemTitleName = [self itemTitleNameLabelWithFrame:itemNameLabelRect itemName:[entity entityName]];
[itemTitleName setTag:TITLEVIEW_TAG];
[cell addSubview:itemTitleName];
// adding 'assigned to' label right under the item name label
UILabel *itemDescriptionLabel = [self itemDescriptionLabelWithFrame:descriptionLabelFrame itemDescription:[entity entityDesctiption]];
[itemDescriptionLabel setTag:DESCRIPTION_TAG];
[cell addSubview:itemDescriptionLabel];
}
I see some troubles in tableView:cellForRowAtIndexPath: logic
It should be:
Dequeue cell
If cell cannot be dequeued - create the new one
Set all cell properties
I mean something like this:
- (MyTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"itemListTableViewCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} // <-- Note there is no else, we should reset properties in both cases
NSManagedObject *managedObject = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [managedObject valueForKey:#"text"];
cell.imageView.image = [managedObject valueForKey:#"image"];
return cell;
}