Unable to set the frame of UITableViewCell's contentView - objective-c

Im trying to set an offset of the contentView of my UITableViewCell. But failed. As you can find in the picture, there is no offset.
The corresponding code is as following.
MyCell.m
#import "MyCell.h"
#implementation MyCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
[backGroundView setBackgroundColor:[UIColor grayColor]];
[self addSubview:backGroundView];
UIView *frontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
[frontView setBackgroundColor:[UIColor greenColor]];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
myLabel.text = #"My Cell";
myLabel.backgroundColor = [UIColor clearColor];
[myLabel setTextAlignment:UITextAlignmentCenter];
[frontView addSubview:myLabel];
[self.contentView addSubview:frontView];
[self bringSubviewToFront:self.contentView];
}
return self;
}
MyTableViewController.m
#import "MyTableViewController.h"
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
CGPoint center = cell.center;
center.x = cell.contentView.bounds.size.width/2 - 20;
[cell.contentView setCenter:center];
NSLog(#"origin.x :%f ,y :%f, width :%f, height :%f",cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width,cell.contentView.frame.size.height);
return cell;
}
log
2012-08-31 18:29:34.075 TableViewCellOffsetTest[16169:207] origin.x :-20.000000 ,y :0.000000, width :320.000000, height :44.000000

Show us your code relating to [cell.contentView setCenter:center]; I suspect you're trying to move the main view of the cell. I wouldn't do that I would add a subview and offset that and add your content views to it. As you're already using a custom method all your changes need to be made there. In addition it's worth noting that you would also want to reset the cells offset each time it's reused as you'll end up moving the content further and further along.

Related

UItableview is moving up and down rather than scrolling inside

I have a UITableView with about 15 cells and setScrollEnabled:YES. The problem I am facing is this:
When I scroll, the entire table view moves. I.e.: If I scroll up, the table view moves up and covers the top bar.
What I'd like to do is to scroll within the inside of UITableView so it doesn't move, only the cells/cell content within it move up/down (possibly even the use of a vertical indicator as well?).
I can't find anything on SO or a Google Search.
What I'm currently using is as follows:
#define NUMBER_ROWS 15
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self.rearTableView respondsToSelector:#selector(setSeparatorInset:)]) {
[self.rearTableView setSeparatorInset:UIEdgeInsetsZero];
}
if (IS_WIDESCREEN) {
[self.rearTableView setScrollEnabled:YES];
[self.rearTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return NUMBER_ROWS;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
MenuCell *cell = (MenuCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (nil == cell)
{
cell = [[MenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.textLabel setHighlightedTextColor:[UIColor whiteColor]];
cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.bounds];
[cell.selectedBackgroundView setBackgroundColor:[UIColor menuSelected]];
if (indexPath.row == 0)
{
cell.textLabel.text = #" Films";
cell.imageView.image = [UIImage imageNamed:#"icon_film.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:#"icon_film_active.png"];
[cell.contentView setBackgroundColor:[UIColor menuGray]];
[cell.textLabel setTextColor:[UIColor menuParent]];
//[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[[cell textLabel] setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:18]];
}
....
}
Images of the issue:
UITableView moving down
UITableView moving up and covering the top bar
Thanks

UICollectionView didn't render properly

The program has a tableview. Each TableViewCell has a UICollectionView. When user drag and drop the one of the categories label, the program will add a UICollectionViewCell which has a UIView. It works well. However, when user scroll the tableview then the UICollectionView won't render properly. The display is as below.
This is the pic before user scroll the view.
http://cl.ly/image/1P0s3Z0m443t
After user scroll the view, there is a promise overlap the sin. The promise cell shouldn't be there.
http://cl.ly/image/3L2M1s3s3F0X
Here is my code of cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
// Configure the cell..
/*
There is no UITableViewCell in the storyboard
*/
ContentViewCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel * cellLabel = nil; //The cellLabel is for rendering the bible verses
if (cell==nil) {
cell = [[ContentViewCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
/*
To Make Dynamic Cell Height
*/
cellLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[cellLabel setNumberOfLines:0];
[cellLabel sizeToFit];
[cellLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[cellLabel setTag:1];
[[cell contentView] addSubview:cellLabel];
}
NSString * text = [verseArray objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGRect rectangular = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName: [UIFont systemFontOfSize:FONT_SIZE]} context:nil];
CGSize size = rectangular.size;
/*
When There Are Rows More Than One Screen
*/
if (!cellLabel) {
cellLabel = (UILabel *)[cell viewWithTag:1];
}
cell.CellContent = text;
[cellLabel setText:text];
[cellLabel setFrame:CGRectMake((self.view.frame.size.width*0.39), 0, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))]; //To Locate The Position Of The Label In The Cell
cell.userInteractionEnabled = TRUE;
return cell;
}
And this is ContentViewCustomTableViewCell
#import "ContentViewCustomTableViewCell.h"
#import "AnswerCollectionViewCell.h"
#implementation ContentViewCustomTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(3, 3, 2, 2);
layout.itemSize = CGSizeMake(30, 10);
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.collectionView = [[AFIndexedCollectionView alloc] initWithFrame:CGRectZero
collectionViewLayout:layout];
[self.collectionView registerClass:[AnswerCollectionViewCell class]forCellWithReuseIdentifier:CollectionViewCellIdentifier];
self.collectionView.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.1];
self.collectionView.showsVerticalScrollIndicator = NO;
[self.contentView addSubview:self.collectionView];
}
return self;
}
- (void)awakeFromNib
{
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void)layoutSubviews
{
[super layoutSubviews];
self.collectionView.frame = CGRectMake(10, 10, 110, 50);
}
-(void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate index:(NSInteger)index
{
self.collectionView.dataSource = dataSourceDelegate;
self.collectionView.delegate = dataSourceDelegate;
self.collectionView.index = index;
[self.collectionView reloadData];
}
#end

Again the Unbalanced calls to begin/end appearance transitions

I know the question has already been asked and answered but I cant find the solution.
I've got this error even if I'm not pushing the incriminated view controller :
Unbalanced calls to begin/end appearance transitions for .
here is my code NewsViewController.m
#import "NewsViewController.h"
#import "XMLToObjectParser.h"
#import "UneNews.h"
#define USE_CUSTOM_DRAWING 1
#interface NewsViewController ()
#end
#implementation NewsViewController
#synthesize imageView;
#synthesize vueDetail;
#synthesize tableauNews,tableViewFluxRSS;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableauDeNews:(NSMutableArray *)tableauDeNews
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
//récupération du tableau de news initialisé
tableauNews = tableauDeNews;
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
tableViewFluxRSS.separatorStyle = UITableViewCellSeparatorStyleNone;
tableViewFluxRSS.rowHeight = 143;
tableViewFluxRSS.backgroundColor = [UIColor clearColor];
}
- (void)viewDidUnload
{
[self setImageView:nil];
[self setVueDetail:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
//Navigation
- (IBAction)goHome:(id)sender{
[self.navigationController popToRootViewControllerAnimated:YES];
}
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here -- for example, create and push another view controller.
[vueDetail loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[[tableauNews objectAtIndex:indexPath.row] detail]]]];
}
//gestion du UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableauNews count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
const NSInteger TOP_LABEL_TAG = 1001;
const NSInteger BOTTOM_LABEL_TAG = 1002;
const NSInteger DATE_LABEL_TAG = 1003;
const NSInteger ANNEE_LABEL_TAG = 1004;
UILabel *topLabel;
UILabel *bottomLabel;
UILabel *dateLabel;
UILabel *anneeLabel;
UILabel *enSavoirPlus;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableViewFluxRSS dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//
// Create the cell.
//
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
//
// Configure the properties for the text that are the same on every row
//
dateLabel = [[UILabel alloc]
initWithFrame:
CGRectMake(6,46,75,57)];
dateLabel.tag = DATE_LABEL_TAG;
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.textColor = [UIColor whiteColor];
dateLabel.font = [UIFont systemFontOfSize:28];
[cell.contentView addSubview:dateLabel];
//
// Configure the properties for the text that are the same on every row
//
anneeLabel = [[UILabel alloc]
initWithFrame:
CGRectMake(6,100,70,57)];
anneeLabel.tag = DATE_LABEL_TAG;
anneeLabel.backgroundColor = [UIColor clearColor];
anneeLabel.textColor = [UIColor whiteColor];
anneeLabel.font = [UIFont systemFontOfSize:31];
[cell.contentView addSubview:anneeLabel];
topLabel =
[[UILabel alloc]
initWithFrame:
CGRectMake(94,5,325,20)];
[cell.contentView addSubview:topLabel];
//
// Configure the properties for the text that are the same on every row
//
topLabel.tag = TOP_LABEL_TAG;
topLabel.backgroundColor = [UIColor clearColor];
topLabel.textColor = [UIColor whiteColor];
topLabel.font = [UIFont systemFontOfSize:18];
//
// Configure the properties for the text that are the same on every row
//
bottomLabel =
[[UILabel alloc]
initWithFrame:
CGRectMake(94,30,325,80)];
bottomLabel.tag = BOTTOM_LABEL_TAG;
bottomLabel.backgroundColor = [UIColor clearColor];
bottomLabel.textColor = [UIColor whiteColor];
bottomLabel.font = [UIFont systemFontOfSize:18];
[bottomLabel setLineBreakMode:UILineBreakModeWordWrap];
[bottomLabel setNumberOfLines:0];
[cell.contentView addSubview:bottomLabel];
//
// Create a background image view.
//
cell.backgroundView =
[[UIImageView alloc] init];
cell.selectedBackgroundView =
[[UIImageView alloc] init];
enSavoirPlus =
[[UILabel alloc]
initWithFrame:
CGRectMake(260,121,200,20)];
[cell.contentView addSubview:enSavoirPlus];
//
// Configure the properties for the text that are the same on every row
//
enSavoirPlus.tag = TOP_LABEL_TAG;
enSavoirPlus.backgroundColor = [UIColor clearColor];
enSavoirPlus.textColor = [UIColor yellowColor];
//topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
enSavoirPlus.font = [UIFont systemFontOfSize:18];
}
else
{
topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG];
bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
dateLabel = (UILabel *)[cell viewWithTag:DATE_LABEL_TAG];
anneeLabel = (UILabel *)[cell viewWithTag:ANNEE_LABEL_TAG];
}
topLabel.text = [[tableauNews objectAtIndex:indexPath.row] titre];
bottomLabel.text = [[tableauNews objectAtIndex:indexPath.row] contenu];
[bottomLabel sizeToFit];
dateLabel.text = [[tableauNews objectAtIndex:indexPath.row] dateDeParution];
anneeLabel.text = [[tableauNews objectAtIndex:indexPath.row] annee];
enSavoirPlus.text = #"En savoir plus...";
//
// Set the background and selected background images for the text.
// Since we will round the corners at the top and bottom of sections, we
// need to conditionally choose the images based on the row index and the
// number of rows in the section.
//
UIImage *rowBackground;
rowBackground = [UIImage imageNamed:#"fd-textes-news.png"];
//selectionBackground = [UIImage imageNamed:#"middleRowSelected.png"];
((UIImageView *)cell.backgroundView).image = rowBackground;
//((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
return cell;
}
//fin gestion UITableView
#end
I think my problem may be due to my implementation of the init
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableauDeNews:(NSMutableArray *)tableauDeNews;
need help
thx
I found the problem:
To create my buttons I copy paste them in the interfaceBuilder,
the copied buttons have kept their old actions, and I had a new IBAction.
My copied buttons had 2 IBactions (one calling a NewsViewController and another calling the right viewController).
The message was poping each time I clicked the copied button.
So check your buttons in the interface Builder, "Unbalanced calls to begin/end appearance transitions" can be caused by buttons having two IBActions, conflictual (eg. pushing two different viewControllers at the same time).

Logic for adding images and scroll icon to the table view

I have a table view, and I am adding four UIImageViews to a row, so in total I have five rows and twenty image views. I am inserting images one by one into the cell. The second row will only start to fill once all four image views are filled.
I need some logic for this. I mean, how can I check which place is next to be filled in the table view and at which event? How can I add images one by one to the UIImageViews? Also, initially only two rows will be shown. After filling these two rows, when images begin to enter into the third row, I need to show an icon adjacent to the last row that is on the screen, to tell the user that there is more data below. Also, is there any event to detect which row is currently the last row on screen?
Also is there any way to individually select the images from a row?
Run this code:-
.h file code
#import <UIKit/UIKit.h>
#interface helpTableViewController : UIViewController {
IBOutlet UITableView *gridTableView;
NSMutableArray *imagesArray;
int widthview,widthview1;
}
#property(nonatomic,retain)NSMutableArray *imagesArray;
#property(nonatomic,retain)UITableView *gridTableView;
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath;
-(void)crossViewButtonClicked:(UIButton *)sender;
#end
.m file code:-
#synthesize gridTableView;
#synthesize imagesArray;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *tempArray=[[NSMutableArray alloc]init];
self.imagesArray=tempArray;
[tempArray release];
[self.imagesArray addObject:[UIImage imageNamed:#"pic1.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic2.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic3.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic4.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic5.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic6.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic1.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic2.png"]];
[self.imagesArray addObject:[UIImage imageNamed:#"pic3.png"]];
widthview=0;
widthview1=0;
}
- (void)viewDidUnload {
self.gridTableView=nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[imagesArray release];
[gridTableView release];
[super dealloc];
}
#pragma mark -
#pragma mark TableView Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=nil;
if (cell == nil) {
cell =[self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath];
}
return cell;
}
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
if (indexPath.row==0) {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
scrollView.contentSize = CGSizeMake(320, 180);
scrollView.backgroundColor=[UIColor clearColor];
scrollView.showsHorizontalScrollIndicator = NO;
[cell.contentView addSubview:scrollView];
for (int i=0; i<4;i++) {
UIView *profileView=[[UIView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)];
profileView.backgroundColor=[UIColor whiteColor];
UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 8, 83, 106)];
[profileimageView setImage:[self.imagesArray objectAtIndex:i]];
profileimageView.tag=i+90;
[profileView addSubview:profileimageView];
[profileimageView release];
[scrollView addSubview:profileView];
widthview=widthview+105;
NSLog(#"%d",widthview);
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(widthview1+10, 12, 95, 155)];
//button.contentMode = UIViewContentModeScaleAspectFill;
button.tag=999999;
[button setBackgroundColor:[UIColor greenColor]];
[button addTarget:self action:#selector(imageButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
widthview=widthview+105;
scrollView.contentSize = CGSizeMake(widthview, 180);
}
if (indexPath.row==1) {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
scrollView.contentSize = CGSizeMake(320, 180);
scrollView.backgroundColor=[UIColor clearColor];
scrollView.showsHorizontalScrollIndicator = NO;
[cell.contentView addSubview:scrollView];
for (int i=0; i<4;i++) {
UIView *profileView=[[UIView alloc]initWithFrame:CGRectMake(widthview1+10, 12, 95, 155)];
profileView.backgroundColor=[UIColor whiteColor];
UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(5, 8, 83, 106)];
// [profileimageView setImage:[UIImage imageNamed:#"gridimage1.png"]];
profileimageView.tag=4+i;
[profileimageView setImage:[self.imagesArray objectAtIndex:3+i]];
[profileView addSubview:profileimageView];
[scrollView addSubview:profileView];
[profileimageView release];
widthview1=widthview1+105;
NSLog(#"%d",widthview1);
}
scrollView.contentSize = CGSizeMake(widthview1, 180);
}return cell;
}
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
#pragma mark -
#pragma mark button methods
-(void)imageButtonClicked:(UIButton *)sender
{
[self.imagesArray replaceObjectAtIndex:0 withObject:[UIImage imageNamed:#"abdulprofilepic.png"]];
[self.gridTableView reloadData];
}
You should keep all of the logic (and data) in your table view controller (UITableViewController subclass). I suggest you have a custom UITableViewCell subclass that can hold 4 images (the cell itself should be responsible for the layout), and the table view controller is responsible for setting it up. In -tableView:cellForRowAtIndexPath: you can look at your collection of images, then just use modular arithmetic to figure out which images should be in the cell for that particular index path.
As for individually selecting images, perhaps you could have your cells know their index path (set as a custom property in -tableView:cellForRowAtIndexPath:), then each have a delegate method telling your controller that a certain image was pressed (for example, -cellAtIndexPath:didSelectImageAtIndex:).
try this,
As there are fix number of images that you want to show in your row i. e. 4
So what you have to do is:-
in .h file
int widthview,widthview1;
in .m file in viewdidload
widthview=0;
widthview1=0;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [self reuseTableViewCellWithIdentifier:CellIdentifier withIndexPath:indexPath];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;//to remove blu selection color
return cell;
}
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell =[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
if (indexPath.row==0) {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
scrollView.contentSize = CGSizeMake(320, 180);
scrollView.backgroundColor=[UIColor clearColor];
scrollView.showsHorizontalScrollIndicator = NO;
[cell.contentView addSubview:scrollView];
for (int i=0; i<4;i++) {
UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)];
[profileimageView setImage:[self.imagesArray objectAtIndex:i]];
[profileimageView release];
[scrollView addSubview:profileimageView];
widthview=widthview+105;
NSLog(#"%d",widthview);
}
scrollView.contentSize = CGSizeMake(widthview, 180);
}
if (indexPath.row==1) {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];
scrollView.contentSize = CGSizeMake(320, 180);
scrollView.backgroundColor=[UIColor clearColor];
scrollView.showsHorizontalScrollIndicator = NO;
[cell.contentView addSubview:scrollView];
for (int i=0; i<4;i++) {
UIImageView *profileimageView=[[UIImageView alloc]initWithFrame:CGRectMake(widthview+10, 12, 95, 155)];
[profileimageView setImage:[self.imagesArray objectAtIndex:4+i]];
[profileimageView release];
[scrollView addSubview:profileimageView];
widthview1=widthview1+105;
NSLog(#"%d",widthview1);
}
scrollView.contentSize = CGSizeMake(widthview1, 180);
}
return cell;
}
This code is for 2 rows do the same thing for next three rows.
Adjust the coordinates according to your requirement.
hope this will save your time.
answer for Also is there any way to individually select the images from a row? :
Give image to button and add that button inside row!!

Center Align text in UITableViewCell problem

I'm kinda new to Objective-C and iPhone development and I've come across a problem when trying to center the text in a table cell. I've searched google but the solutions are for an old SDK bug that has been fixed and these don't work for me.
Some code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = #"Please center me";
cell.textLabel.textAlignment = UITextAlignmentCenter;
return cell;
}
The above doesn't center the text.
I have also tried the willDisplayCell method:
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.textAlignment = UITextAlignmentCenter;
}
and I've tried some of the old posted solutions:
UILabel* label = [[[cell contentView] subviews] objectAtIndex:0];
label.textAlignment = UITextAlignmentCenter;
return cell;
None of these have any effect on the text alignment. I have run out of idea's any help would be most appreciated.
Cheers in advance.
Don't know if it helps your specific problem, however UITextAlignmentCenter does work if you use initWithStyle:UITableViewCellStyleDefault
It doesn't work because the textLabel is only as wide as it needs to be for any given text. (UITableViewCell moves the labels around as it sees fit when set to the UITableViewCellStyleSubtitle style)
You can override layoutSubviews to make sure the labels always fill the cell's entire width.
- (void) layoutSubviews
{
[super layoutSubviews];
self.textLabel.frame = CGRectMake(0, self.textLabel.frame.origin.y, self.frame.size.width, self.textLabel.frame.size.height);
self.detailTextLabel.frame = CGRectMake(0, self.detailTextLabel.frame.origin.y, self.frame.size.width, self.detailTextLabel.frame.size.height);
}
Be sure to keep the height/y-position the same, because as long as the detailTextLabel's text is empty textLabel will be vertically centered.
Use this code:
cell.textLabel.textAlignment = NSTextAlignmentCenter;
Above code will work.
Dont use UITextAlignmentCenter, it is deprecated.
This hack will center the text when using UITableViewCellStyleSubtitle.
Load both text labels with your strings, then do this before returning the cell. It might be simpler to just add your own UILabels to each cell, but I was determined to find another way...
// UITableViewCellStyleSubtitle measured font sizes: 18 bold, 14 normal
UIFont *font = [UIFont boldSystemFontOfSize:18]; // measured after the cell is rendered
CGSize size = [cell.textLabel.text sizeWithFont:font];
CGSize spaceSize = [#" " sizeWithFont:font];
float excess_width = ( cell.frame.size.width - 16 ) - size.width;
if ( cell.textLabel.text && spaceSize.width > 0 && excess_width > 0 ) { // sanity
int spaces_needed = (excess_width/2.0)/spaceSize.width;
NSString *pad = [#"" stringByPaddingToLength:spaces_needed withString:#" " startingAtIndex:0];
cell.textLabel.text = [pad stringByAppendingString:cell.textLabel.text]; // center the text
}
font = [UIFont systemFontOfSize:14]; // detail, measured
size = [cell.detailTextLabel.text sizeWithFont:font];
spaceSize = [#" " sizeWithFont:font];
excess_width = ( cell.frame.size.width - 16 ) - size.width;
if ( cell.detailTextLabel.text && spaceSize.width > 0 && excess_width > 0 ) { // sanity
int spaces_needed = (excess_width/2.0)/spaceSize.width;
NSString *pad = [#"" stringByPaddingToLength:spaces_needed withString:#" " startingAtIndex:0];
cell.detailTextLabel.text = [pad stringByAppendingString:cell.detailTextLabel.text]; // center the text
}
In CustomTableViewCell.m:
- (void)layoutSubviews {
[super layoutSubviews];
self.textLabel.frame = CGRectMake(0, self.textLabel.frame.origin.y, self.contentView.frame.size.width, self.textLabel.frame.size.height);
}
In the method table:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = #"Title";
cell.textLabel.textAlignment = UITextAlignmentCenter;
return cell;
}
If needed, the same thing can be repeated for self.detailTextLabel
In same situation I created custom UITableViewCell with a custom label:
MCCenterTextCell.h file:
#import <UIKit/UIKit.h>
#interface MCCenterTextCell : UITableViewCell
#property (nonatomic, strong) UILabel *mainLabel;
#end
MCCenterTextCell.m file:
#import "MCCenterTextCell.h"
#interface MCCenterTextCell()
#end
#implementation MCCenterTextCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.accessoryType = UITableViewCellAccessoryNone;
self.selectionStyle = UITableViewCellSelectionStyleGray;
_mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 30)];
_mainLabel.font = BOLD_FONT(13);
_mainLabel.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:_mainLabel];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
You could use code to center text
cell.indentationLevel = 1;
cell.indentationWidth = [UIScreen mainScreen].bounds.size.width/2-10;
In case one wish to align the text to the right, I've had success adapting the solution described here.
cell.transform = CGAffineTransformMakeScale(-1.0, 1.0);
cell.textLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);
cell.detailTextLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);
Here is what works for me...
NSString *text = #"some text";
CGSize size = [text sizeWithAttributes:#{NSFontAttributeName:SOME_UIFONT}];
[cell setIndentationLevel:1];
[cell setIndentationWidth:(tableView.frame.size.width - size.width)/2.0f];
cell.textLabel.font = SOME_UIFONT;
[cell.textLabel setText:text];