UITableViewCell not showing first MPMoviePlayerController video on the cell in iOS 7? - ios

I am trying to add video on UITableViewCell the video is adding perfectly but the problem is that when I add second video the first cell on UITableViewCell is got black and second cell showing video.
I want all my cell showing video first frame(video picture)
But it is showing only last one(Last cell)
Then I add UIButton to play every cell video it is working perfectly.
My Problem is that I want all my cell showing video first frame(video picture) on every UITableViewCell.
But problem is if only one cell add then it is showing video first picture perfectly. Then I click button and its started video perfectly but when I add second video the first one got black and second one work perfectly.
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section==0) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"listData"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil] autorelease];
cell.selectedBackgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:#".png"] ];
cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#".png"] ];
[cell setBackgroundColor:[UIColor clearColor]];
UILabel * lbl =[[UILabel alloc]initWithFrame:CGRectMake(330, 80, 135, 41)];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #"Play Video Here";
[cell.contentView addSubview:lbl];
NSInteger x = [[NSUserDefaults standardUserDefaults] integerForKey:#"cellRowNumber"];
NSLog(#"x is %d",x);
if (x<1) {
x = 0;
}
NSURL* url = [UIAppDelegate.videoUrlsArray objectAtIndex:indexPath.row];
NSLog(#"selected tableview row is %d",indexPath.row);
NSInteger p = indexPath.row ;
NSLog(#"P is %d",p);
secView2 =[[UIView alloc]initWithFrame:CGRectMake(84,5,600,170)];
secView2.tag = indexPath.row;
secView2.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:secView2];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayer.view.frame = CGRectMake(0,0,600,170);
moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
[moviePlayer prepareToPlay];
moviePlayer.shouldAutoplay = NO;
[secView2 addSubview:moviePlayer.view];
playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
playBtn.tag = indexPath.row;
playBtn.frame = CGRectMake(0,0,600,170);
[playBtn setBackgroundColor:[UIColor clearColor]];
playBtn.tag = indexPath.row;
[playBtn addTarget:self action:#selector(didButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[secView2 addSubview:playBtn];
}
// moviePlayer.contentURL = [UIAppDelegate.videoUrlsArray objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//[self.moviePlayer pause];
return cell;
}
else {
// Nothing
}
//cell.textLabel.text = [UIAppDelegate.youTubeUrlsArray objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
}
-(void)didButtonTouchUpInside:(id)sender{
UIButton *btn = (UIButton *) sender;
CGRect buttonFrameInTableView = [btn convertRect:btn.bounds toView:table];
NSIndexPath *indexPath = [table indexPathForRowAtPoint:buttonFrameInTableView.origin];
NSLog(#"%d",indexPath.row);
secView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 768, 960)];
secView.backgroundColor = [UIColor redColor];
[self.view addSubview:secView];
NSURL* url = [UIAppDelegate.videoUrlsArray objectAtIndex:indexPath.row];
// Now set up the movie player controller and play the movie.
player = [[MPMoviePlayerController alloc] initWithContentURL: url];
[[player view] setFrame: CGRectMake(0, 50, 768, 910)]; // frame must match parent view
[secView addSubview: [player view]];
[player play];
//btn_hideSecview = [UIButton buttonWithType:UIButtonTypeCustom];
btn_hideSecview =[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 45)];
btn_hideSecview.backgroundColor = [UIColor greenColor];
// btn_hideSecview.frame = CGRectMake(600, 0, 70, 45);
[btn_hideSecview setBackgroundColor:[UIColor whiteColor]];
[btn_hideSecview addTarget:self action:#selector(method_Hide_Secview) forControlEvents:UIControlEventTouchUpInside];
[secView addSubview:btn_hideSecview];
}
Any idea or suggestions would be highly welcome.

As I understand you have created "secView2" and "moviePlayer" as local properties, that means you use only one instance of these classes. For each invoke of table delegate method cellForRowAtIndexPath - create new object of MoviePlayer and UIView. If you need access outside of that delegate, prepare some DataSource NSMutableArray which will store that instances.
MPMoviePlayerController * newPlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
and
UIView *secView2 =[[UIView alloc]initWithFrame:CGRectMake(84,5,600,170)];

Related

Header image not resizable in expandable table

I am pretty new to Xcode and this simple problem has been driving me mad! I have created an expandable table that works fine. This is some of the code on a UIView subclass for the section that expands when you tap on a cell:
- (id)initWithFrame:(CGRect)frame WithTitle: (NSString *) title Section:(NSInteger)sectionNumber delegate: (id <SectionView>) Delegate
{
self = [super initWithFrame:frame];
if (self) {
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(discButtonPressed:)];
[self addGestureRecognizer:tapGesture];
self.userInteractionEnabled = YES;
self.section = sectionNumber;
self.delegate = Delegate;
CGRect LabelFrame = CGRectMake(100, 100, 100, 100);
LabelFrame.size.width -= 100;
CGRectInset(LabelFrame, 1.0, 1.0);
//BUTTON
CGRect buttonFrame = CGRectMake(LabelFrame.size.width, 0, 100, LabelFrame.size.height);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = buttonFrame;
//[button setImage:[UIImage imageNamed:#"carat.png"] forState:UIControlStateNormal];
//[button setImage:[UIImage imageNamed:#"carat-open.png"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(discButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
self.discButton = button;
//My IMAGE
NSString *imageName = #"gradient1.png";
UIImage *myImage = [UIImage imageNamed:imageName];
UIImageView *sectionHeaderView = [[UIImageView alloc] initWithImage:myImage];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
imageView.frame = CGRectMake(20,50,100,100);
[self addSubview:sectionHeaderView];
self.headerBG = sectionHeaderView;
//HEADER LABEL
UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(22, 12, sectionHeaderView.frame.size.width, 35.0)];
label.textAlignment = NSTextAlignmentLeft;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor darkGrayColor];
label.shadowOffset = CGSizeMake(0.0, -1.0);
label.text = title;
label.font = [UIFont fontWithName:#"AvenirNext-Bold" size:20.0];
sectionHeaderView.backgroundColor = [UIColor clearColor];
//label.textAlignment = UITextAlignmentLeft;
[self addSubview:label];
self.sectionTitle = label;
}
return self;
}
I have a custom image on the cell #"gradient1.png" but I don't seem to be able to resize it? Here is the header code in the UITableViewController:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection: (NSInteger)section
{
SectionInfo *array = [self.sectionInfoArray objectAtIndex:section];
if (!array.sectionView)
{
NSString *title = array.category.name;
array.sectionView = [[SectionView alloc] initWithFrame:CGRectMake(0, 10, self.tableView.bounds.size.width, 0) WithTitle:title Section:section delegate:self];
}
return array.sectionView;
}
Sorry if this is a trivial question, your help is greatly appreciated!
I don't see where you are trying to resize the image so I can't offer any help in why it is not resizing, but I found this confusing:
//My IMAGE
NSString *imageName = #"gradient1.png";
UIImage *myImage = [UIImage imageNamed:imageName];
UIImageView *sectionHeaderView = [[UIImageView alloc] initWithImage:myImage];
UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
imageView.frame = CGRectMake(20,50,100,100);
[self addSubview:sectionHeaderView];
self.headerBG = sectionHeaderView;
In this part of code you create two imageviews, then you resize one and add the other to the cell (I'm assuming cell) subviews. Is it possible that you are trying to resize the view that you never added as a subview of the cell?
Also, resizing should happen inside - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.
Make sure that you are resizing the proper view, ten make sure you are resizing it in the proper place.

Table view button index cell

I have one table view in that cell is custom. I am adding two button on each cell of table view. when I click on first button at same time second button from same cell is changing its image. for that I have methods as editQuantity and Cancelorder. using #sel. I am getting an issue that when i click on first button insted of changing same cell another button its changing another cells button also when I scroll table view its losses all selected button
Here Is My Code--
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
simpleTableIdentifier = #"MenuNameCell";
MenuNameCell *cell = (MenuNameCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell== nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuNameCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
NSLog(#"---------new cell agin");
}
else
{
NSArray *arrayView = [cell.contentView subviews];
for (UIView *vTemp in arrayView)
{
[vTemp removeFromSuperview];
}
NSLog(#"---No New Cell hiiii");
// Setting Tag To UIButton
_checkButton = (UIButton *)[cell.contentView viewWithTag:indexPath.row];
_cancelButton = (UIButton *)[cell.contentView viewWithTag:indexPath.row];
}
// Creating Label Menu Name
_nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 11, 82, 21)];
_nameLabel.backgroundColor = [UIColor clearColor];
_nameLabel.text = [_hotel._orderedMenus objectAtIndex:indexPath.row];
// Creating Label Menu Cost
_amountMenu = [[UILabel alloc] initWithFrame:CGRectMake(167, 13, 44, 21)];
_amountMenu.backgroundColor = [UIColor clearColor];
_amountMenu.text = [[_hotel._menuPrices objectAtIndex:indexPath.row] stringValue];
// Creating Text Field For Order Quantity
_textFieldQuantity = [[UITextField alloc] initWithFrame:CGRectMake(125,14,42,21)];
_textFieldQuantity.userInteractionEnabled = NO;
_textFieldQuantity.text = [[_hotel._selectedQuantity objectAtIndex:indexPath.row] stringValue];
// Creating Button For Check Order
_checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_checkButton setFrame:CGRectMake(232, 13, 25, 28)];
[_checkButton setTag:indexPath.row];
_checkButton.titleLabel.tag = indexPath.row;
[_checkButton setBackgroundImage:[UIImage imageNamed:#"edit.png"]forState:UIControlStateNormal];
[_checkButton addTarget:self action:#selector(editQuantity:) forControlEvents:UIControlEventTouchUpInside];
// Creating Button For CANCEL Order
_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_cancelButton setFrame:CGRectMake(265, 13, 25, 28)];
[_cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel.png"] forState:UIControlStateNormal];
[_cancelButton setTag:indexPath.row];
_cancelButton.titleLabel.tag = indexPath.row;
[_cancelButton addTarget:self action:#selector(cancelOreder:) forControlEvents:UIControlEventTouchUpInside];
// Adding All To Content View
[cell.contentView addSubview:_nameLabel];
[cell.contentView addSubview:_amountMenu];
[cell.contentView addSubview:_textFieldQuantity];
[cell.contentView addSubview:_checkButton];
[cell.contentView addSubview:_cancelButton];
//objc_setAssociatedObject(_checkButton, iindex, indexPath,OBJC_ASSOCIATION_RETAIN );
return cell;
}
-(void)editQuantity:(id)sender{
button = (UIButton *)sender;
row = button.tag;
col = button.titleLabel.tag;
NSLog(#"Check Button index is %d",row);
NSLog(#"cehck title is %d",col);
_textFieldQuantity.userInteractionEnabled = YES;
UIImage *buttonImage = [UIImage imageNamed:#"edit_over.png"];
[_checkButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
UIImage *buttonImageCancel = [UIImage imageNamed:#"check.png"];
[_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
_cancelButton.tag = 0;
}
-(void)cancelOreder:(id)sender{
button = (UIButton *)sender;
row = button.tag;
NSLog(#"The Row Selected iS At Cancel Order ISSSS----%d", row);
if (_cancelButton.tag == 0){
_textFieldQuantity.userInteractionEnabled = NO;
UIImage *buttonImageCancel = [UIImage imageNamed:#"check_over.png"];
[_cancelButton setBackgroundImage:buttonImageCancel forState:UIControlStateNormal];
UIImage *buttonImageCancel1 = [UIImage imageNamed:#"cancel.png"];
[_cancelButton setBackgroundImage:buttonImageCancel1 forState:UIControlStateNormal];
UIImage *buttonImage = [UIImage imageNamed:#"edit.png"];
[_checkButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
_cancelButton.tag = 1;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"iHomeDelivery" message:#"Do You Want To Cancel the Order" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
The problem is that the default tag value for any control is set to 0 and you have used the _cancelButton.tag = 0;. Just change the value to 10 and then _cancelButton.tag = 1; change 1 to 11.
This will solve your problem :)
As I see _checkButton, _nameLabel, _amountMenu, _textFieldQuantity & _cancelButton all are instance variable of your view controller class.
In cellForRowAtIndexPath: as you are assigning new objects to all these, they will refere to objects of last cell displayed. Means all these will point to components from last cell.
Hence changing background images for _checkButton & _cancelButton will affect for buttons in last cell.
Also in cellForRowAtIndexPath: while creating these buttons, you are setting background image for them, hence after scrolling your button images are getting changed. Remember cellForRowAtIndexPath: gets called for a row each time that row becomes visible after getting hidden while scrolling.
You need to maintain state of buttons in separate array, so that you can reset state for buttons in cellForRowAtIndexPath: after dequeuing.

iOS switching between UIWebViews causing memory warning

Hi i have created a custom tab bar that hides and shows certain web views depending on which tab your on i'm currently loading all three web views on an operation queue then making them visable and invisable depending on the selected tab. problem is its so slow to load and scroll and i'm getting a recieved memory warning
heres what i have done so far
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *subviews = [cell subviews];
for(UIView *subview in subviews) if([subview tag] == 4) [subview removeFromSuperview];
UIView *loadingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];
UIView *sendingMessage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
UIActivityIndicatorView *sendingSpinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(25, 15, 50, 50)];
UILabel *sendingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 65, 100, 20)];
int sendingViewLeft = (loadingView.frame.size.width/2) - (sendingMessage.frame.size.width/2);
int sendingViewTop = 160 - (sendingMessage.frame.size.height/2);
loadingView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
sendingMessage.frame = CGRectMake(sendingViewLeft, sendingViewTop, sendingMessage.frame.size.width, sendingMessage.frame.size.height);
sendingMessage.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
sendingMessage.layer.cornerRadius = 10;
sendingSpinner.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
sendingLabel.textColor = [UIColor whiteColor];
sendingLabel.backgroundColor = [UIColor clearColor];
sendingLabel.text = #"Loading...";
sendingLabel.textAlignment = UITextAlignmentCenter;
sendingLabel.font = [UIFont boldSystemFontOfSize:12];
sendingLabel.shadowColor = [UIColor blackColor];
sendingLabel.shadowOffset = CGSizeMake(0, -1);
[sendingSpinner startAnimating];
[sendingMessage addSubview:sendingLabel];
[sendingMessage addSubview:sendingSpinner];
[loadingView addSubview:sendingMessage];
[loadingView setTag:4];
cell.textLabel.text = #"";
if([self.webViewStatus isEqualToString:#"FALSE"]){
[cell addSubview:loadingView];
self.tableView.userInteractionEnabled = NO;
} else {
[cell addSubview:self.fixturesWebView];
[cell addSubview:self.resultsWebView];
[cell addSubview:self.tablesWebView];
self.tableView.userInteractionEnabled = YES;
}
if(self.selectedTabNumber == 1){
self.fixturesWebView.alpha = 1;
self.resultsWebView.alpha = 0;
self.tablesWebView.alpha = 0;
}
if(self.selectedTabNumber == 2){
self.fixturesWebView.alpha = 0;
self.resultsWebView.alpha = 1;
self.tablesWebView.alpha = 0;
}
if(self.selectedTabNumber == 3){
self.fixturesWebView.alpha = 0;
self.resultsWebView.alpha = 0;
self.tablesWebView.alpha = 1;
}
return cell;
}
First off, you should consider creating a custom cell rather than creating it at the tableview datasource. Second, when you are getting the webview content, consider using Asynchronous calls to avoid lock ups. This could be done in a block too. If you write delegate methods on the custom cell, you can let the tableview know when things are loaded or not etc.
But it looks like you are making it do too much work when the tableview needs to draw a cell.

Memory leak when loading image to custom cell in UITableView

In my iPhone app, having a UITableView with custom cells. Which contains UILabels and UIImageViews. But I am getting memory leak when I assign the image to the image view. Here is the code. Leak is in the method cellForRowAtIndexPath: I have mentioned the leak percentage. Please check the code, what went wrong here? Please help.
// UIMenuItemCell.h
#class UIMenuitemImage;
#interface UIMenuItemCell : UITableViewCell{
UILabel *cellItemName;
UIImageView *cellitemImage;
}
#property (nonatomic, retain) UILabel *cellItemName;
#property (nonatomic, retain) UIImageView *cellitemImage;
// UIMenuItemCell.m
#import "UIMenuItemCell.h"
#implementation UIMenuItemCell
#synthesize cellItemName, cellitemImage, cellItemButton, cellItemProgress;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
// cellitemImage = [[UIMenuitemImage alloc]init];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
// MenuScreenVC.m
- (UIMenuItemCell *) getCellContentView:(NSString *)cellIdentifier {
CGRect CellFrame = CGRectMake(0, 0, 150, 60);
CGRect Label1Frame = CGRectMake(20, 23, 98, 30);
CGRect imgFrame = CGRectMake(20, 48, 110, 123);
CGRect btnFrame = CGRectMake(25, 136, 100, 30);
CGRect progressFrame = CGRectMake(25, 140, 100, 21);
UILabel *lblTemp;
UIImageView *itemImg;
UIButton *itemBtn;
UIProgressView *itemProgView;
UIMenuItemCell *cell = [[[UIMenuItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
cell.frame = CellFrame;
//Initialize Label with tag 1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.tag = 1;
lblTemp.textColor=[UIColor colorWithRed:139.0f/255.0f green:69.0f/255.0f blue:19.0f/255.0f alpha:1.0f];
lblTemp.textAlignment = UITextAlignmentCenter;
lblTemp.backgroundColor = [UIColor clearColor];
lblTemp.font = [UIFont systemFontOfSize:13.0];
[cell.contentView addSubview:lblTemp];
[lblTemp release];
//Initialize ImageView
itemImg = [[UIImageView alloc]initWithFrame:imgFrame];
itemImg.tag = 2;
[cell.contentView addSubview:itemImg];
[itemImg release];
//Initialize Button
itemBtn = [[UIButton alloc]initWithFrame:btnFrame];
itemBtn.frame = btnFrame;
itemBtn.tag = 3;
itemBtn.titleLabel.textColor = [UIColor blueColor];
itemBtn.titleLabel.font = [UIFont systemFontOfSize:9.0];
[cell.contentView addSubview:itemBtn];
[itemBtn release];
//Initialize ProgressView
itemProgView = [[CustomProgressView alloc]initWithFrame:progressFrame];
itemProgView.tag = 4;
//[cell.contentView addSubview:itemProgView];
[itemProgView release];
return cell;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [NSString stringWithFormat:#"Cell%d", indexPath.row];
UIMenuItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [self getCellContentView:CellIdentifier];
cell.transform = CGAffineTransformMakeRotation(M_PI_2);
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.cellItemName = (UILabel *)[cell viewWithTag:1];
cell.cellitemImage = (UIImageView *)[cell viewWithTag:2];
cell.cellItemButton = (UIButton *)[cell viewWithTag:3];
cell.cellItemProgress = (UIProgressView *)[cell viewWithTag:4];
DataBaseClass *itemObj = [appDelegate.itemArray objectAtIndex:indexPath.row];
__autoreleasing NSString *imageLocalFilePath = nil;
if ([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:#"NotAvailable"]) {
cell.cellItemProgress.hidden = YES;
cell.cellItemButton.hidden = NO;
imageLocalFilePath = [NSString stringWithFormat:#"%#",[tempItemLocalNotAvailPath objectAtIndex:indexPath.row]];
NSString *date = [self changeDateFormat:itemObj.itemReleaseDate];
[cell.cellItemButton setTitle:date forState:UIControlStateNormal];
cell.cellItemButton.userInteractionEnabled = NO;
cell.userInteractionEnabled = NO;
[cell.cellItemButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"not_available_bttn_bck_img"] forState:UIControlStateNormal];
}else if ([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:#"Available"]){
cell.cellItemButton.userInteractionEnabled = YES;
cell.userInteractionEnabled = YES;
cell.cellItemProgress.hidden = YES;
[cell.cellItemButton setTitle:#"" forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"available_bttn_img_normal"] forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"available_bttn_img_pressed"] forState:UIControlStateHighlighted];
[cell.cellItemButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
[cell.cellItemButton addTarget:self action:#selector(confirmationAlert:) forControlEvents:UIControlEventTouchUpInside];
imageLocalFilePath = [NSString stringWithFormat:#"%#",[tempItemLocalAvailPath objectAtIndex:indexPath.row]];
}else if ([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:#"Active"]) {
cell.cellItemButton.userInteractionEnabled = YES;
cell.userInteractionEnabled = YES;
cell.cellItemProgress.hidden = YES;
[cell.cellItemButton setTitle:#"" forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"active_bttn_img_normal"] forState:UIControlStateNormal];
[cell.cellItemButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
[cell.cellItemButton addTarget:self action:#selector(alert) forControlEvents:UIControlEventTouchUpInside];
imageLocalFilePath = [NSString stringWithFormat:#"%#",[tempItemLocalAvailPath objectAtIndex:indexPath.row]];
}else if([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:#"Downloading"]) {
imageLocalFilePath = [NSString stringWithFormat:#"%#",[tempItemLocalAvailPath objectAtIndex:indexPath.row]];
[cell.contentView addSubview:myprogressView];
cell.cellItemButton.hidden = YES;
}
if ([imageLocalFilePath isEqualToString:#""]) {
[cell.cellitemImage setImage:[UIImage imageNamed:#"item01.png"]];
}else {
[cell.cellitemImage setImage:[UIImage imageWithContentsOfFile:imageLocalFilePath]];
}
cell.cellItemName.text = [NSString stringWithFormat:#"%#",[tempItemNameArray objectAtIndex:indexPath.row]];
for (UIProgressView *prog in cell.contentView.subviews) {
if ([prog isKindOfClass:[UIProgressView class]]){
if (prog.progress == 1) {
[prog removeFromSuperview];
cell.cellItemButton.hidden = NO;
DataBaseClass *itemObj = [appDelegate.itemArray objectAtIndex:indexPath.row];
NSString *imageLocalFilePath = nil;
if ([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:#"Available"]){
cell.cellItemButton.userInteractionEnabled = YES;
cell.userInteractionEnabled = YES;
cell.cellItemProgress.hidden = YES;
[cell.cellItemButton setTitle:#"" forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"available_bttn_img_normal"] forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"available_bttn_img_pressed"] forState:UIControlStateHighlighted];
[cell.cellItemButton addTarget:self action:#selector(confirmationAlert:) forControlEvents:UIControlEventTouchUpInside];
}else if ([[tempitemStatusArray objectAtIndex:indexPath.row] isEqualToString:#"Active"]) {
cell.cellItemButton.userInteractionEnabled = YES;
cell.userInteractionEnabled = YES;
cell.cellItemProgress.hidden = YES;
[cell.cellItemButton setTitle:#"" forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"active_bttn_img_normal"] forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"active_bttn_img_normal"] forState:UIControlStateHighlighted];
[cell.cellItemButton addTarget:self action:#selector(alert) forControlEvents:UIControlEventTouchUpInside];
}
imageLocalFilePath = [NSString stringWithFormat:#"%#",itemObj.availableLocalIconPath];
if ([imageLocalFilePath isEqualToString:#""]) {
[cell.cellitemImage setImage:[UIImage imageNamed:#"item01.png"]];
}else {
[cell.cellitemImage setImage:[UIImage imageWithContentsOfFile:imageLocalFilePath]];
}
cell.cellItemName.text = [NSString stringWithFormat:#"%#",[tempItemNameArray objectAtIndex:indexPath.row]];
[cell.contentView reloadInputViews];
}
}
}
}else {
for (UIProgressView *prog in cell.contentView.subviews) {
if ([prog isKindOfClass:[UIProgressView class]]){
if (prog.progress == 1) {
[prog removeFromSuperview];
cell.cellItemButton.hidden = NO;
cell.cellItemButton.userInteractionEnabled = YES;
cell.userInteractionEnabled = YES;
[cell.cellItemButton setTitle:#"" forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"active_bttn_img_normal"] forState:UIControlStateNormal];
[cell.cellItemButton setBackgroundImage:[UIImage imageNamed:#"active_bttn_img_normal"] forState:UIControlStateHighlighted];
[cell.cellItemButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
[cell.cellItemButton addTarget:self action:#selector(alert) forControlEvents:UIControlEventTouchUpInside];
}
}
}
}
return cell;
}
In your method
- (UIMenuItemCell *) getCellContentView:(NSString *)cellIdentifier
Use:
UIMenuItemCell *cell = [[[UIMenuItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
Edited to respond to comments:
The proper way to release the iVars is in the dealloc method. If it's not getting called, that means your UIMenuItemCells aren't being released. Possibly this is because of the way that your implementation works; you're creating a reusable cell for every row in your table view. I think that if you mark a cell for reuse it doesn't get released until the table view is released.
You could test this by seeing if all the memory is released when your table view is released (if this happens in your app). That would mean that you don't actually have a leak, just that your app uses a lot of memory (most likely unnecessarily). You could also test to see if the memory increases when you scroll rows that you've already created back onto the screen, because in that case I think you will be reusing your cells, so the memory shouldn't increase.
You could also try initializing your table view cells with nil for the cell identifier so that they get released when they scroll off screen.
You should probably try to rework your code though so that you do actually reuse your cells. Otherwise scrolling is most likely going to be pretty choppy.
I meet similar issue, and finally resolve it.
because your table cell would be reused later,
so just call removeFromSuperview before add subviews to the cell.
I had a memory spike issue while loading image to imageView.
below code fixed my issue,
Note: This will reduce the image quality.
In my case i'm loading image in smaller image view.
let resizedImage = image.aspectFittedToHeight(100)
resizedImage.jpegData(compressionQuality: 0.2)
return resizedImage
}
and add this Extension
extension UIImage {
func aspectFittedToHeight(_ newHeight: CGFloat) -> UIImage {
let scale = newHeight / self.size.height
let newWidth = self.size.width * scale
let newSize = CGSize(width: newWidth, height: newHeight)
let renderer = UIGraphicsImageRenderer(size: newSize)
return renderer.image { _ in
self.draw(in: CGRect(origin: .zero, size: newSize))
}
}
}

the event touchUpInside of a UIButton don't work

there is only 1 cell in my UITableView, and I add a UIButton to the cell.
set the action when touchUpInside event happens. But it doesn't work at all. Meanwhile, if
i set the trigger as touchDown event instead of touchUpInside, it will fire the action i set.
I search the reason by google, someone says that the problem maybe caused by the superview's dimension. That is the size of the button is beyond its superview.
so I add a UIView to contain the button, the code is as follow:
self.ageDisplay = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
UIImage *ageImage = [UIImage imageNamed:#"long_btn_click.png"];
[self.ageDisplay setImage:ageImage forState:UIControlStateSelected];
[self.ageDisplay setFrame:CGRectMake(10, 5, 145, 34)];//(10, 320, 145, 25)];
[self.ageDisplay setTitle:#"请选择您的年龄" forState:UIControlStateNormal];
[self.ageDisplay setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.ageDisplay setTag:3];
[self.ageDisplay addTarget:self action:#selector(showSelectionPage:) forControlEvents:UIControlEventTouchDown];
self.buttonsSubView = [[UIView alloc] initWithFrame:CGRectMake(0, 310, 320, 106)];
//self.buttonsSubView.backgroundColor = [UIColor blueColor];
[self.buttonsSubView setUserInteractionEnabled:YES];
[self.buttonsSubView addSubview:self.ageDisplay];
the above code is in ViewDidLoad function.
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *sugPageCell = #"SuggestionPageCell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:sugPageCell];
if (nil == cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sugPageCell];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell addSubview:self.sugTitle];
[cell addSubview:self.sugSpecification];
[cell addSubview:self.suggestion];
self.buttonsSubView.userInteractionEnabled = YES;
[cell addSubview:self.buttonsSubView];
/*
[cell addSubview:self.ageDisplay];
[cell addSubview:self.genderDisplay];
[cell addSubview:self.submit];
*/
return cell;
}
However, this doesn't help.
The problem has confused me for 2 days.
I will appreciate if anyone can provide some indication.
Thank you.
It's because your buttonSubView view is outside the cell's frame.
So even if the button is visible (because the cell's clipsToBounds is set to NO), it will not receive touch events from the cell. Why do you set this view's vertical position to 310 ?
If you try this :
self.buttonsSubView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 106)];
The event should fire as you want.
I had this problem recently. I guess there must may be an object of UITapGesture added to your UITableView. If so, please set the property cancelsTouchesInView of the UITapGesture object to NO.
For example:
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:target action:tapAction];
gesture.cancelsTouchesInView = NO;
[self addGestureRecognizer:gesture];

Resources