Different first row for a UITableView - iphone

I need a UITableView whose first row should show a logo, while other rows should show the other table datas.
I tried the following code:
if(indexPath.row == 0) {
//Code for imageview
} else {
//Code to display array content
}
My problem is that I don't get the first array item in the table view.
Edited::
if (indexPath.row == 0) {
CGRect myImageRect = CGRectMake(120.0f, 5.0f, 70.0f, 55.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:#"logo.png"]];
[cell.contentView addSubview:myImage];
}
else {
NSDictionary *dict = [menuitems objectAtIndex:indexPath.row];
cell.textLabel.text =[dict objectForKey:#"category"];
}

Try to use an headerview. Use the following code to place logo at the top of row.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
UIImageView *myimgvw = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[myimgvw setImage:[UIImage imageNamed:#"logo.png"]];
[myView addSubview:myimgvw];
return myView;
}

You haven't shown your code that displays the array content, but I guess you are using something like
[dataArray objectAtIndex:indexPath.row];
But because you are showing the image at row 0, you need to offset this by -1. In other words, the second row in your table will have indexPath.row = 1, but you need that to be objectAtIndex:0.

Then you need to put use indexPath-1 to show text instead of indexPath in else part of above code snippet.

Related

Button is not responsive on headerview of UITableView

I am creating headers for my tableview.And adding view on that header.
In that view there are two imageviews and a button
The button is not responsive(a very small area on the top part is responsive)
Why this is happening,Please help me ?
I have tried with some SO answers- like "setAutoresizingMask:UIViewAutoresizingNone"
But nothing is working
Here is my code
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// ADDING A VIEW
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 310, 39.0)];
header.backgroundColor = [UIColor clearColor];
header.userInteractionEnabled = YES;
[header setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
// ADDING IMAGE VIEW
UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 310, 39.0)];
image.image = [UIImage imageNamed:#"headertop.png"];
[image setUserInteractionEnabled:TRUE];
[header addSubview:image];
// ADDING ANOTHER IMAGEVIEW (beneath the view's frame)
UIImageView *downImage = [[UIImageView alloc]initWithFrame:CGRectMake(0 , 39, 310, 23)];
downImage.image = [UIImage imageNamed:#"headerbottom.png"];
[header addSubview:downImage];
// ADDING BUTTON
UIButton *placeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[placeBtn addTarget:self
action:#selector(goToRestPage:)
forControlEvents:UIControlEventTouchUpInside];
placeBtn.tag = section;
placeBtn.backgroundColor = [UIColor yellowColor];
placeBtn.frame = CGRectMake(75,20, 150, 20);
[header addSubview:placeBtn];
return header;
}
Use this Method. I think you miss this method.
-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30.0;
}
Or this method already exists so increase the height of the header.
-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 80.0;
}
Please do this :
User Interaction Enabled to TRUE from your XIB. by default its FALSE.
Thanks,

Overlapping UITextView when add text to UITextView

I created UIViewController in Storyboard and added tableview as a Subview. Its working fine. In that tableview I added cells programatically and each cell contains UITextFields or UITextView. UITextFields are working fine. But UITextViews have some troubles. EX: When I add text to UITextView then bellow UITextViews are overlapped. why is that ? I tried to sort out whole day. But still I couldn't. :(
case cellResponseNote:
cell = [tableView.tableView dequeueReusableCellWithIdentifier:CellIdentifierResponseNote];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierResponseNote];
cell.frame = CGRectMake(0, 0, cell.frame.size.width, cellNotesHeight);
//cell.frame = CGRectMake(0, 0, cell.frame.size.width, cellNotesHeight);
if (!self.txtVwResponseNote) {
// self.txtVwResponseNote = [[UITextView alloc] initWithFrame:cell.frame];
self.txtVwResponseNote = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cellNotesHeight)];
[self.txtVwResponseNote setScrollEnabled:NO];
[self.txtVwResponseNote setAutoresizingMask:UIViewAutoresizingNone];
[self.txtVwResponseNote setDelegate:self];
[self.txtVwResponseNote setFont:[UIFont systemFontOfSize:14]];
[self.txtVwResponseNote setInputAccessoryView:self.keyboardToolbar];
[self setupMailData:indexPath.row];
}
}
self.txtVwResponseNote.editable = !(self.isMailForReview);
// [cell.contentView addSubview:self.txtVwResponseNote];
[cell addSubview:self.txtVwResponseNote];
contentSubview = self.txtVwResponseNote;
In this image bottom You can see three cells. ( bellow Footer) When I add text value for top text view. Then overlap the next text view.
Bellow image you can see only two textviews. Bottom text view is not showing.
So what should I do ?
First of all as an answer try setting the height of the cell by following method.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
case cellResponseNote:
return cellNotesHeight;
default:
return defaultHeight;
}
Also I recommend you not to share the textview property. Instead try modifying your code like this and see whether your code works.
case cellResponseNote:
cell = [tableView.tableView dequeueReusableCellWithIdentifier:CellIdentifierResponseNote];
UITextView *txtVwResponseNote = nil;
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierResponseNote];
cell.frame = CGRectMake(0, 0, cell.frame.size.width, cellNotesHeight);
//cell.frame = CGRectMake(0, 0, cell.frame.size.width, cellNotesHeight);
//if (!self.txtVwResponseNote) {
// self.txtVwResponseNote = [[UITextView alloc] initWithFrame:cell.frame];
txtVwResponseNote = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cellNotesHeight)];
[txtVwResponseNote setScrollEnabled:NO];
[txtVwResponseNote setAutoresizingMask:UIViewAutoresizingNone];
[txtVwResponseNote setDelegate:self];
[txtVwResponseNote setFont:[UIFont systemFontOfSize:14]];
[txtVwResponseNote setInputAccessoryView:self.keyboardToolbar];
[txtVwResponseNote setTag:100];
//}
} else {
txtVwResponseNote = (UITextView *)[cell viewWithTag:100]
}
[self setupMailData:indexPath.row];
txtVwResponseNote.editable = !(self.isMailForReview);
// [cell.contentView addSubview:txtVwResponseNote];
[cell addSubview:self.txtVwResponseNote];
contentSubview = self.txtVwResponseNote;
It gets repeated because you added it to the cell as a subview, and then reuse the cell. Add it as a accessoryView of the cell instead, and set it to nil for the cell's that dont need it.
cell.accessoryView = self.txtVwResponseNote;
And in all other cells, set accessoryView to nil.
cell.accessoryView = nil;

Dynamically add image view in Custom cell and assign image to it

I am facing lots of problem.. i am new to ios development so any help is appreciated.
Problem 1st
I want to display N number of image-view+label over yellow and blue image In custom cell.
I have to parse that how many Number of Image view will be shown on every row and according to that i have to assign image to every image-view in my custom cell. how can i do that?
I fetch image links from database and store it into array but how can i apply links to particular image-view in row.
Just need hints to build above User Interface in iOS.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifierLeft = #"lefthand";
static NSString *CellIdentifierRight = #"righthand";
if (indexPath.row % 2 == 1) // to get two custom cell vice versa.
{
LeftCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierLeft];
if (LeftCell == nil)
{
LeftCell = [[customCellLeft alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierLeft row:indexPath.row];
LeftCell.scrollView = [[cellScrollViewClass alloc] initWithFrame:CGRectMake(0,14,1024,187)];
}
LeftCell.videoImageView1.tag=[ImageLinkArray objectAtIndex:indexPath.row];
LeftCell.label1.text=[TitleArray objectAtIndex:indexPath.row];
return LeftCell;
}
else
{
RightCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierRight];
if(RightCell == nil)
{
RightCell =[[customCellRight alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierRight];
RightCell.scrollView = [[cellScrollViewClass alloc] initWithFrame:CGRectMake(0,14,1024,187)];
}
RightCell.videoImageView1.tag=[ImageLinkArray objectAtIndex:indexPath.row];
RightCell.label1.text=[TitleArray objectAtIndex:indexPath.row];
return RightCell;
}
Custom cell class code...
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier row:(int)row
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
int indexNumber; // variable to store Array index number
int VideoNumber=3; // imageview in particular cell + Train Engine.
scrollView = [[cellScrollViewClass alloc] initWithFrame:CGRectMake(33,14,958,187)]; // Scrollview
scrollView.scrollEnabled=YES;
scrollView.userInteractionEnabled=YES;
scrollView.showsHorizontalScrollIndicator=NO;
scrollView.contentSize = CGSizeMake((335*VideoNumber),187); // To make ContentSize for item to scroll
for (int i = 0; i < VideoNumber; i++)
{ // for loop to add 2 wagon view and 1 engine view on Left hand side Custom cell.
if(i==0) // to check first item must be train Engine
{
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 18;
frame.size.width=186;
frame.size.height= 162;
UIView *EngineView = [[UIView alloc]initWithFrame:frame];
UIImageView *engineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 6, 186, 162)];
UIImage *Image = [UIImage imageNamed:#"loco_left.png"];
engineImageView.image = Image;
[EngineView addSubview:engineImageView];
[scrollView addSubview:EngineView]; // add 1st imageview that is Train Engine to first of scrollview
}
else
{ // else it must be wagon of train.
indexNumber=row*(VideoNumber-1)+(i-1);// to get Index Number of array to to display Images as per cell number
NSLog(#"at Normal row index number is %d",indexNumber);
CGRect frame;
frame.origin.x = (385*i)-432;
frame.origin.y = 18;
frame.size.width=385;
frame.size.height= 163;
UIView *wagon = [[UIView alloc]initWithFrame:frame];
UIImageView *wagonImage = [[UIImageView alloc]initWithFrame:CGRectMake(230, 6, 385, 163)];
UIImage *Image = [UIImage imageNamed:#"wagon_left.png"];
wagonImage.image = Image;
[wagon addSubview:wagonImage];
UIView *videoviewContainer = [[UIView alloc]initWithFrame:CGRectMake(15, 30, 190 , 200)];
videoImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 190, 100)];
UIImage *bgImage = [UIImage imageNamed:#"video.png"];
videoImageView2.image = bgImage;
videoviewContainer.contentMode = UIViewContentModeLeft; // set Video Image view to left hand side of wagon UIView
videoImageView2.tag=indexNumber;
[videoviewContainer addSubview:videoImageView2];
UITapGestureRecognizer *video = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(VideoItemOne:)];
[videoImageView2 addGestureRecognizer:video];
[videoImageView2 setMultipleTouchEnabled:YES];
[videoImageView2 setUserInteractionEnabled:YES];
UIView *textUiView = [[UIView alloc]initWithFrame:CGRectMake(208,28, 150 , 187)];
textUiView.contentMode = UIViewContentModeRight;
label2 = [[UILabel alloc]initWithFrame:CGRectMake(28,10, 150 , 87)];
label2.text=[TitleArray objectAtIndex:indexNumber];
label2.backgroundColor = [UIColor clearColor];
label2.textColor=[UIColor redColor];
label2.numberOfLines = 4;
[textUiView addSubview:label2];
[wagonImage addSubview:textUiView];
[wagonImage addSubview:videoviewContainer];
[scrollView addSubview:wagon];
}
}
[self.contentView addSubview:scrollView];
}
return self;
}
-(void)populateWithImage:myImage andText:myText
{
[videoImageView2 setImageWithURL:[NSURL URLWithString:myImage]
placeholderImage:[UIImage imageNamed:#"video.png"]];
}
Your custom cell should have the following: (note, you have to use a better naming convention and i am just giving an example)
Background Imageview (bgImage)
Main image View (mainImage)
UI Label (mainLabel)
In the init for this cell, based on the indexpath.row, either assign the left or right train image.
Also set the frames for main image view and the label.
MYCustomCell * cell;
if(indexPath.row %2)
{
cell = [tableView dequeueReusableCellWithIdentifier:"OddCell"];
if (cell == nil)
{
cell = [[MYCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierLeft row:indexPath.row];
}
}
else {
cell = [tableView dequeueReusableCellWithIdentifier:"EvenCell"];
if (cell == nil)
{
cell = [[MYCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierLeft row:indexPath.row];
}
}
[cell populateWithImage:myImage andText:myText]; //myImage and myText are to be gotten from the data source which in your case is ImageLinkArray and TitleArray
return cell;

UITableView headerview keeps repeating

I am making an iPhone app. In this app I have a UITableView which has a UITableView header. This is how my code looks like.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(30, 0, 308, 50)];
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"sx_home_tablehead.png"]];
self.tableViewAppointments.tableHeaderView = view;
And this is how it looks like on my iPhone
And this is what I want to achieve
Hope that anyone can help me !
Kind regards !
Because you're specifying explicitly the frame of the view. You should not do a terrible hack like this. Use this piece of code instead:
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"header.png"]];
self.tableView.tableHeaderView = imgView;
If you want to add it as a table view section header, you need to add it as follows,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(30, 0, 308, 50)];
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"sx_home_tablehead.png"]];
//or,
UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"sx_home_tablehead.png"]];
return view;
}
You can define the height for header in
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return height;
}

How to set Background image in UITableView cell?

I have Created custom cell.And i have table view with Grouped style.Now i want to put Backgroung image to each cell of different section.How to set the background image of custom cell.
Here is the code for creating the custom cell.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 305, 90)];
// view.backgroundColor=[UIColor darkGrayColor];
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(11, 9, 61, 55)];
// NSLog(#"imageArray%#",[play.imageArray count]);
NSString *img=[play.imageArray objectAtIndex:indexPath.row];
NSLog(#"img=%#",img);
imageV.image = [UIImage imageNamed:img];
[view addSubview:imageV];
[imageV release];
UILabel *lblAchievementName = [[UILabel alloc] initWithFrame:CGRectMake(90, 10, 168, 21)];
lblAchievementName.text = [play.listData objectAtIndex:indexPath.row];
lblAchievementName.backgroundColor=[UIColor clearColor];
lblAchievementName.textColor=[UIColor orangeColor];
[lblAchievementName setFont:[UIFont fontWithName:#"Arial Black" size:16.5]];
[view addSubview:lblAchievementName];
[lblAchievementName release]
[cell.contentView addSubview:view];
return cell;
Now how to set the backgroung image for this custom view?
You can try this code that will help you.
Put this code in cellForRowAtIndexPath method
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:#"categorytab1.png"];
cell.backgroundView = av;
set thru setBackgroundView method.Form the ImageView with ur need Image aand set that as,
[cell setBackgroundView:urBgImgView];
You can use following code for different image in different section.
UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(11, 9, 61, 55)];
// NSLog(#"imageArray%#",[play.imageArray count]);
NSString *img=[play.imageArray objectAtIndex:indexPath.row];
NSLog(#"img=%#",img);
if (indexPath.section == 0) {
imageV.image = [UIImage imageNamed:img0];
}
else if (indexPath.section == 1) {
imageV.image = [UIImage imageNamed:img1];
}
else if (indexPath.section == 2) {
imageV.image = [UIImage imageNamed:img2];
}
[view addSubview:imageV];
[imageV release];
i'd rather to add a CALayer or UIImageView to be the backgroud view. Because i used to set the BackgroundColor, but it never workd for me. so i changed my mind by this way.