How to Get webview from UITableView didselectrowatindexpath? - ios

I have a UITableview in this view i created uiwebview for each cell. When i tap on a cell i want the view of UIWebView. How to achieve this??
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = #"Cell";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
[[cell.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
cell.accessoryType = UITableViewCellAccessoryNone;
UIView *customView = [[UIView alloc]init];
customView.transform = CGAffineTransformMakeRotation(M_PI_2);
[customView setFrame:CGRectMake(0, 0, 1024, 768)];
[customView setBackgroundColor:[UIColor clearColor]];
UIWebView *menuWeb = [[UIWebView alloc]init];
[menuWeb setFrame:CGRectMake(0, 0, 768, 1024)];
[menuWeb setDelegate:self];
[menuWeb setUserInteractionEnabled:YES];
[menuWeb.scrollView setScrollEnabled:NO];
[menuWeb setScalesPageToFit:YES];
NSURL *url = [NSURL fileURLWithPath:[urlArr objectAtIndex:indexPath.row]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[menuWeb loadRequest:request];
[customView addSubview:menuWeb];
[cell.contentView addSubview:customView];
[cell setBackgroundColor:[UIColor clearColor]];
return cell;
}

in cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
....
UIView *customView = [[UIView alloc]init];
customView.tag = 100;
....
return cell;
}
and in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UIView *customView = [cell.contentView viewWithTag:100];
for (id view in customView.subViews) {
if ([view isKindOfClass:[UIWebView class]]) {
UIWebView *menuWeb = (UIWebView *)view;
}
}
}
hope this will help

There are a couple of ways you can achieve this.
The simplest way would be to assign a tag to the webview and then retreive it when the cell is tapped.
When you create the cell add:
[menuWeb setTag:SOME_CONSTANT_NUMBER];
Then inside the webview delegate's didSelectRowAtIndexPath method retrieve the cell and it's webView:
UITableViewCell *cell = [tableVoew cellForRowAtIndexPath:indexPath];
UIWebView *webView = [cell viewWithTag:SOME_CONSTANT_NUMBER];

Try this one, in cellForRowAtIndexPath tag CustomView and webview so that you can retrieve using tag-
......
customView.tag = 111;
......
webView.tag = 222;
.......
Then in didSelectRowAtIndexPath
UITableViewCell *cell = (UITableViewCell *) [tableView cellForRowAtIndexPath:indexPath];
UIView *customView = [cell.contentView viewWithTag:111];
Use viewWithTag to find out webview
UIWebView *wView = [customView viewWithTag:222];
Or iterate over custom view to find out webview
UIWebView *wView = nil;
for ((id) subView in [customView subviews]) {
if ([subView isKindOfClass:[UIWebView class]]){
//Found WebView
wView =(UIWebView *) subView;
break;
}
}

Related

Custom cell contents overlapping in UITableView

I know this question is already asked many times, but my problem is some different.
I am creating a UIView and a UIImageView programmatically in cell's content view. When TableView appear first time it looking perfect, but when i scroll down and up , this seems overlapped.
Screenshot of without scroll:
Screenshot after scroll:
Code that i follow:
viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.viewForContents.frame.origin.x, cell.viewForContents.frame.origin.y-10, cell.viewForContents.frame.size.width, 45)];
viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:1];
[cell.contentView addSubview:viewForHead];
UIImageView *imageViewForDP = [[UIImageView alloc]initWithFrame:CGRectMake(viewForHead.frame.origin.x-50, viewForHead.frame.origin.y-8, 60,60 )];
imageViewForDP.image = [UIImage imageNamed:#"dog_1.png"];
//[cell.viewForContents addSubview:imageViewForDP];
imageViewForDP.layer.cornerRadius = 30;
imageViewForDP.clipsToBounds = YES;
[viewForHead addSubview:imageViewForDP];
Please get me out from this problem . Thanks
Use this into your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
if ([cell.contentView subviews]){
for (UIView *subview in [cell.contentView subviews]) {
[subview removeFromSuperview];
}
}
You are adding your viewForHead as a subview each time the cell gets dequeued. So you're adding them on top of each other.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CELL"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"CELL"] autorelease];
// This is where you CREATE your cell contents.
viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.viewForContents.frame.origin.x, cell.viewForContents.frame.origin.y-10, cell.viewForContents.frame.size.width, 45)];
viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:1];
[cell.contentView addSubview:viewForHead];
UIImageView *imageViewForDP = [[UIImageView alloc]initWithFrame:CGRectMake(viewForHead.frame.origin.x-50, viewForHead.frame.origin.y-8, 60,60 )];
imageViewForDP.image = [UIImage imageNamed:#"dog_1.png"];
// [cell.viewForContents addSubview:imageViewForDP];
imageViewForDP.layer.cornerRadius = 30;
imageViewForDP.clipsToBounds = YES;
imageView.tag = 1
[viewForHead addSubview:imageViewForDP];
}
// this is where you UPDATE your viewForHead image and any other elements inside your cell
UIImageView *imageView = [cell.contentView viewWithTag:1];
imageView.image = // your new image
return cell;
}
Subclassing your UITableViewCell and building your layout with a xib would be even better, then you could just access the cells properties directly. A much cleaner solution.
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier#"CELL"];
if (cell == nil) {
cell = [[MyCustomCell alloc] init]; // you ID is set in interface builder
}
cell.imageView.image = // your new image here.
cell.someLabel.text = #"some new text here"
This problem is because of table view cell gets reuse and you are adding a view again on cell.
Try below code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier"] ;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
UIView *viewForHead = (UIView *)[cell.contentView viewWithTag:1];
if (viewForHead==nil) {
viewForHead = [[UIView alloc]initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, 20)];
viewForHead.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:255.0/255.0 blue:16.0/255.0 alpha:0.5];
viewForHead.tag = 1;
[cell.contentView addSubview:viewForHead];
}
return cell;}

UIButton auto SelfSizing in objective C

I have some issues with how to auto self sizing native UIButton in ios. OnClick UIButton then button is auto resize with changeable text.
If anyone has a solution so help me.
thanks in advance
code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
NSLog(#"Hide tag");
UIButton *myView = [self.textView viewWithTag:-1];
[myView removeFromSuperview];
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
NSLog(#"Show tag");
UIButton *myView = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 40.0f, 20.0f)];
[myView setBackgroundColor:[UIColor redColor]];
[myView setTag:-1];
[self.textView addSubview:myView];
NSLog(#"add tag");
self.textView.text = [NSString stringWithFormat:#"%#%#%#", self.textView.text,cell.textLabel.text, #"\n"];
}
}

UITableViewCell content does not show

I have delegate method that retrieves the content for each cell like this:
-(void)getRetrievedShopContentStickerName:(NSArray *)stickerNameArray price:(NSArray *)stickerPriceArray profileBackgroundImage:(NSArray *)stickerProfileBackgroundImageArray profile:(NSArray *)stickerProfileImageArray profileMenuImage:(NSArray *)stickerProfileMenuImageArray designerName:(NSArray *)designerNameArray
{
self.stickerPriceArray = [[NSMutableArray alloc] initWithArray:stickerPriceArray];
self.stickerProfileBGImageArray = [[NSMutableArray alloc] initWithArray:stickerProfileBackgroundImageArray];
NSLog(#"sticker price: %#", self.stickerPriceArray);
NSLog(#"sticker bg: %#", self.stickerProfileBGImageArray);
[self.shopTableView reloadData];
}
This delegate methods logs out a working an array as expected. This is code I used for displaying table view content:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.stickerPriceArray.count;
}
- (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];
AsyncImageView *backgroundImage = [[AsyncImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; //does cell frame get before this method?
[backgroundImage setTag:123];
[cell.contentView addSubview:backgroundImage];
UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 250)];
[priceLabel setTag:124];
[priceLabel setFont:[UIFont fontWithName:#"Helvetica-Light" size:30.0f]];
[priceLabel setTextColor:[UIColor blackColor]];
[cell.contentView addSubview:priceLabel];
}
[(AsyncImageView *)[cell.contentView viewWithTag:123] loadImageWithTypeFromURL:[NSURL URLWithString:[self.stickerProfileBGImageArray objectAtIndex:indexPath.row]] contentMode:UIViewContentModeScaleAspectFit imageNameBG:nil];
[(UILabel *)[cell.contentView viewWithTag:124] setText:[self.stickerPriceArray objectAtIndex:indexPath.row]];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
The only change I get after reloading the table view in the delegate method is the height of each tableviewcell. Why doesn't my contents show? What did I do wrong?
Thanks in advance
I accidentally set my cell's identifier in the storyboard so the if (cell == nil) was never called.

How to change cell text color with out hiding the separator?

In my my application I want to change the cell text color with out disappearing the cell separator.And I am using the following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
static NSString *identifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
[selectedBackgroundView setBackgroundColor:[UIColor clearColor]]; // set color here
[cell setSelectedBackgroundView:selectedBackgroundView];
cell.backgroundColor=[UIColor clearColor];
cell.textLabel.highlightedTextColor = [UIColor redColor];
[cell setOpaque:NO];
}
cell.textLabel.text=[contentArray objectAtIndex:indexPath.row];
return cell;
}
But when I clicked the cell the cell separator also is disappearing ? How to change the text color without hiding the separator ?
It seems the cell separators are a problem for a lot of people. So, I would say rather than doing what I suggested to disable the selection, it would be easier to set the cell separator to 'none' and manage the separator yourself in the background and selected background views:
- (void)viewDidLoad
{
[super viewDidLoad];
// ...
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
static NSString *identifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
[selectedBackgroundView setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:selectedBackgroundView];
UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];
[backgroundView setBackgroundColor:[UIColor clearColor]];
[cell setBackgroundView:backgroundView];
UIView *selectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectMake(tableView.separatorInset.left, cell.frame.size.height - 1, cell.frame.size.width - tableView.separatorInset.left, 1)];
UIView *backgroundSeparator = [[UIView alloc] initWithFrame:selectedBackgroundSeparator.frame];
selectedBackgroundSeparator.backgroundColor = backgroundSeparator.backgroundColor = tableView.separatorColor;
[selectedBackgroundView addSubview:selectedBackgroundSeparator];
[backgroundView addSubview:backgroundSeparator];
cell.textLabel.highlightedTextColor = [UIColor redColor];
[cell setOpaque:NO];
}
cell.textLabel.text=[contentArray objectAtIndex:indexPath.row];
return cell;
}
Alternatively, you can use the default cell separator, and instead just add your own top and bottom separators to the selectedBackgroundView:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
static NSString *identifier = #"cell";
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
UIView *selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
[selectedBackgroundView setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:selectedBackgroundView];
UIView *topSelectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectMake(tableView.separatorInset.left, 0, cell.frame.size.width - tableView.separatorInset.left, 1)];
UIView *selectedBackgroundSeparator = [[UIView alloc] initWithFrame:CGRectOffset(topSelectedBackgroundSeparator.frame, 0, cell.frame.size.height)];
topSelectedBackgroundSeparator.backgroundColor = selectedBackgroundSeparator.backgroundColor = tableView.separatorColor;
[selectedBackgroundView addSubview:selectedBackgroundSeparator];
[selectedBackgroundView addSubview:topSelectedBackgroundSeparator];
cell.textLabel.highlightedTextColor = [UIColor redColor];
[cell setOpaque:NO];
}
cell.textLabel.text=[contentArray objectAtIndex:indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
for(id view in cell.containtView.subview) {
if([view isKindaOfClass:[UILabel class]])
UILabel* titleLabel = (UILabel*)view;
[titleLabel setTextColor:[UIColor whiteColor]]; // any you want
}
}
Rather than setting the cell's selectedBackgroundView to a clear view, would simply not allowing the cell to be highlighted when selected accomplish what you want? It will prevent the cell and separators from changing automatically based on the selection, but you will have to manage recognizing the tap gesture and highlighting the label text yourself.
Remove the selectedBackgroundView from your code.
You then need to implement tableView:shouldHighlightRowAtIndexPath: in your UITableViewDelegate:
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO; // do what's appropriate based on the indexPath
}

How to change tableview cell image?

I have table view in side bar which is similar to facebook app. In my sidebar tableview i have imageview in each row. I want the imageview will be changed when i clicked the cell and it will be retain while for selecting another cell. Here is my 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.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
UIImageView * cellImg = [[UIImageView alloc]initWithFrame:CGRectMake(5,8,259, 60)];
[cellImg setImage:[UIImage imageNamed:[menuArr objectAtIndex:indexPath.row]]];
[contentView addSubview:cellImg];
[contentView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contentView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
delegate.sideBarSelectedIndex = indexPath.row;
NSLog(#"delegate.sideBarSelectedIndex: %d",delegate.sideBarSelectedIndex);
[UIView commitAnimations];
if (self.sidebarDelegate) {
NSObject *object = [NSString stringWithFormat:#"ViewController%d", indexPath.row];
[self.sidebarDelegate sidebarViewController:self didSelectObject:object atIndexPath:indexPath];
[delegate.firstLensePageObj timerFunction:indexPath.row];
}
}
Try this in your didSelectRowAtIndexPath delegate method :-
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:#"yourImage"];
Try this in cellForRowAtIndexPath.
UIImageView *selectedImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Your Image"]];
cell.selectedBackgroundView = selectedImageView;
Need to capture the selected indexpath in didSelectRowAtIndexPath: method and need to reload the tableview in that method. In cellForRowAtIndexPath need to check the selected index with current index.
- (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.contentView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIView * contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 60)];
UIImageView * cellImg = [[UIImageView alloc]initWithFrame:CGRectMake(5,8,259, 60)];
if (delegate.sideBarSelectedIndex == indexpath.row){
//Selected cell
[cellImg setImage:[UIImage imageNamed:[selectedMenuArr objectAtIndex:indexPath.row]]];
}else {
//Unselected cell
[cellImg setImage:[UIImage imageNamed:[menuArr objectAtIndex:indexPath.row]]];
}
[contentView addSubview:cellImg];
[contentView setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contentView];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
delegate.sideBarSelectedIndex = indexPath.row;
NSLog(#"delegate.sideBarSelectedIndex: %d",delegate.sideBarSelectedIndex);
[UIView commitAnimations];
if (self.sidebarDelegate) {
NSObject *object = [NSString stringWithFormat:#"ViewController%d", indexPath.row];
[self.sidebarDelegate sidebarViewController:self didSelectObject:object atIndexPath:indexPath];
[delegate.firstLensePageObj timerFunction:indexPath.row];
}
}

Resources