I have UITableView cell, in a UITableView. When i am trying to add new value from search bar list selected that time that value i am adding into table and same time displying the popover on that newly added cell but some time its working perfect but no of times it display anywhere i am using cell.frame my code is bellow for the same i am attach the expected result screen short also please any one know then help me
- (void)loadPopOver:(id)idDataObj {
NSDictionary *dictDataObj = (NSDictionary*)idDataObj;
//add the popOverControl
OrdersDetailPopViewController *orderDetailPopVC = [[OrdersDetailPopViewController alloc] init];
orderDetailPopVC.idParentDelegate = self;
CGSize sizeObj = [orderDetailPopVC initView:5 totalTextArea:0 dataForTableViews:[dictDataObj valueForKey:TAG_DATASOURCE] titles:[dictDataObj valueForKey:TAG_TITLES]];
if (sizeObj.height == 0 && sizeObj.width == 0) {
NSLog(#"API Error : Incomplete data sent.");
return;
}
//Point from where to show the popOverControl
CGRect cgRect;
if (indexPathLastSelected.section == -1 ) {
if([_isFromSearchBar isEqualToString:#"FROM_SEARCH_BAR"])
{
UITableViewCell *cell = [tableViewLeft cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
cgRect = cell.frame;
//cgRect.origin.y = 190;
cgRect.origin.y = 25;
//cgRect.origin.x =270;
}
else if([_isFromRightTable isEqualToString:#"FROM_RIGHT_TABLE"])
{
UITableViewCell *cell = [tableViewLeft cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
cgRect = cell.frame;
}
}
else {
UITableViewCell *cell = [tableViewLeft cellForRowAtIndexPath:indexPathLastSelected];
cgRect = cell.frame;
}
popOverFormInput = [[UIPopoverController alloc] initWithContentViewController:orderDetailPopVC];
popOverFormInput.contentViewController = orderDetailPopVC;
[popOverFormInput setPopoverContentSize:sizeObj];
popOverFormInput.delegate = self;
if (self.view.window != nil)
[popOverFormInput presentPopoverFromRect:cgRect inView:tableViewLeft permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
You have a lot of funky code in your project which I highly suggest you revise. For now though, I suggest going for simplicity and using a method like this
//Get the cell's frame that you're interested in
UITableViewCell *cell = [tableViewLeft cellForRowAtIndexPath:selectedindexPath]
//and then use that cell's frame to position your popover over it.
[popOverFormInput presentPopoverFromRect:[cell frame] inView:cell.contentView
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
As you are trying to popup using the CGRect, It might not the superView's frame Or else you are somewhere misusing of the frame Value.
Better get(Note) the NSIndePath of the tableView. Later can use it to popup from it >
For Ex:
- (void)loadPopOver:(id)idDataObj {
NSIndexPath *selectedindexPath;
.
.
.
if([_isFromSearchBar isEqualToString:#"FROM_SEARCH_BAR"])
selectedindexPath = // [tableViewLeft cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
.
.
.
// Try this to pop your View
[self.popOver presentPopoverFromRect:[tableViewLeft rectForRowAtIndexPath:selectedindexPath] inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
At the time of calling method i just put the delay
and i fixed the issue
[self performSelector:#selector(showPopOver) withObject:nil afterDelay:0.20];
Related
I have a screen, where we are displaying questions with YES or NO. For Yes Or No we have used UISegment Control.
When a value is changes in the UISegment Control the scroll is moved to Top of the screen, when we have more questions and when we select last or but last as shown in the image.
Below is the code used to render the controls.
Link to the videos how the output looks https://imgur.com/nvn1Exw .
Please help me how can i remove the scroll movement on selection of new value in segment control.
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return timeOutQuestions.count;
}
- (UITableViewCell* )tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
static NSString* questionTableIdentifier=#"SegmentCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier: questionTableIdentifier];
if(cell == nil){
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:questionTableIdentifier];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
}
cell.separatorInset = UIEdgeInsetsMake(0.0 , cell.bounds.size.width , 0.0, -cell.bounds.size.width);
Question* currentQuestion=[[shared getQuestions:questionSet] objectAtIndex:indexPath.row];
#try{
UISegmentedControl *segment = [[cell.contentView subviews] objectAtIndex:0];
segment.selectedSegmentIndex = [[questionSegmentControl objectAtIndex:indexPath.row] integerValue];
[segment addTarget:self action:#selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
UILabel *label = [[cell.contentView subviews] objectAtIndex:1];
label.text = currentQuestion.questionText;
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}#catch(NSException* ex){
NSLog(#"%#",ex.reason);
}
if(changeOffSet && indexChangedValue.row > 2){
[self.questionTableView setContentOffset:tableContentOffSet animated:NO];
[UIView setAnimationsEnabled:true];
changeOffSet = false;
}
return cell;
}
- (IBAction)segmentSwitch:(UISegmentedControl *)sender {
NSInteger selectedSegment = sender.selectedSegmentIndex;
CGPoint senderOriginInTableView = [questionTableView convertPoint:CGPointZero fromView:sender];
NSIndexPath *indexPath = [questionTableView indexPathForRowAtPoint:senderOriginInTableView];
indexChangedValue = indexPath;
tableContentOffSet = self.questionTableView.contentOffset;
[UIView setAnimationsEnabled:false];
changeOffSet = true;
}
https://imgur.com/nvn1Exw
I think you are doing the scrolling programatically with this
[self.questionTableView setContentOffset:tableContentOffSet animated:NO];
I think you need to rework it a bit. Stop changing the content offset and see how it goes. Also, maybe you need to get rid of this line
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
which I think also interferes with the scrolling.
No storyboard involve in this project
The most problematic part i cant figure out where to solve this.
Im Using SDK.
_msgTableView = [[UITableView alloc] init];
_msgTableView.backgroundColor = [UIColor blackColor];
_msgTableView.delegate = self;
_msgTableView.dataSource = self;
_msgTableView.separatorInset = UIEdgeInsetsZero;
_msgTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_msgTableView.showsVerticalScrollIndicator = NO;
[self.view addSubview:_msgTableView];
this function are called on NSNotification
CGRect moveToRect = CGRectMake(-(msgFrame.origin.x+ msgFrame.size.width), msgFrame.origin.y, msgFrame.size.width, msgFrame.size.height);
[_msgTableView setFrame:moveToRect];
this is called in extended view
[_msgTableView sizeWith:CGSizeMake(screenW, 250)];
[_msgTableView layoutAbove:_bottomView margin:kDefaultMargin];
this is called when new message notify by Notification
[_msgDatas addObject:msg];
if (_msgDatas.count >= 500)
{
NSRange range = NSMakeRange(_msgDatas.count - 100, 100);//只保留最新的100条消息
NSArray *temp = [_msgDatas subarrayWithRange:range];
[_msgDatas removeAllObjects];
[_msgDatas addObjectsFromArray:temp];
[_msgTableView reloadData];
}
else
{
[_msgTableView beginUpdates];
NSIndexPath *index = [NSIndexPath indexPathForRow:_msgDatas.count - 1 inSection:0];
[_msgTableView insertRowsAtIndexPaths:#[index] withRowAnimation:UITableViewRowAnimationAutomatic];
[_msgTableView endUpdates];
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_msgDatas.count-1 inSection:0];
if (indexPath.row < [_msgTableView numberOfRowsInSection:0])
{
[_msgTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
this is the Cell For row at function
MsgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"LiveTextMessageCell"];
if (cell == nil)
{
cell = [[MsgTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"LiveTextMessageCell"];
}
id msg = _msgDatas[indexPath.row];
if ([msg isKindOfClass:[ILVLiveTextMessage class]])
{
ILVLiveTextMessage *textMsg = (ILVLiveTextMessage *)msg;
[cell configMsg:textMsg.sendId ? textMsg.sendId : [[ILiveLoginManager getInstance] getLoginId] msg:textMsg.text];
}
if ([msg isKindOfClass:[ILVLiveCustomMessage class]])
{
ILVLiveCustomMessage *customMsg = (ILVLiveCustomMessage *)msg;
[cell configTips:customMsg.sendId];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
this is the ConfigWith function
CGFloat selfW = [UIScreen mainScreen].bounds.size.width ;
CGFloat selfH = 30;
UIFont *msgFont = [UIFont fontWithName:#"Superclarendon" size:12];//Helvetica-Bold
_nickname = profile.nickname;
NSString *showInfo = [NSString stringWithFormat:#"%#: %#",profile.nickname, text];
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:showInfo];
[attrStr addAttribute:NSForegroundColorAttributeName value:kColorGreen range:NSMakeRange(0, profile.nickname.length+1)];//+1是因为有个冒号
[attrStr addAttribute:NSForegroundColorAttributeName value:kColorWhite range:NSMakeRange(profile.nickname.length+1, text.length+1)];//+1是因为有个空格
[attrStr addAttribute:NSFontAttributeName value:msgFont range:NSMakeRange(0, showInfo.length)];//加粗
_msgLabel.attributedText = attrStr;
CGSize labelsize = [self textHeightSize:showInfo maxSize:CGSizeMake(selfW - kDefaultMargin*2, selfH * 3) textFont:msgFont];
[_msgLabel setFrame:CGRectMake(_msgLabel.frame.origin.x, _msgLabel.frame.origin.y, labelsize.width + kDefaultMargin, labelsize.height)];
[self setFrame:CGRectMake(0, 0, selfW, labelsize.height)];
_height = labelsize.height;
_msgLabel.hidden = NO;
_tipsLabel.hidden = YES;
As per my understanding when a new message notification has come, you are not maintaining the height of the cells according to the data. Hence they seem to be overlapped.
Now you have to maintain the height of the new notification, And the data is obtained should be added to your main array(data source). And hence can be easily coordinated.and cells are not get overlapped.
For maintaining the height of the cells, use "heightForRowAtIndexPath "delegate functions of the table view and Maintain the height of a particular cell accordingly.
And one more thing, please be sure that single message will be in the single cell. This will manage accordingly.
Please make sure that a single message was loaded by a single cell,and the height of cell is correct. It looks like you created lots of UILabels in the same cell.
Try using visual constraints instead of setFrame may help!
I found the solution.
i create a nib file as an extension to tableview cell and i register as nib
but im accepting #mayanksengar answer for giving me good insight
I just created a xib file and .h and .m
register xib to table view
[_msgTableView registerNib:[UINib nibWithNibName:#"customCell2" bundle:nil] forCellReuseIdentifier:#"customCell2"];
finally used it
NSString *cellIdentifier = #"customCell2";
customCell2 *cell= [_msgTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell){
[_msgTableView registerNib:[UINib nibWithNibName:#"customCell2" bundle:nil] forCellReuseIdentifier:#"customCell2"];
}
I am working with Custom UITableCell and dynamic UITextView in my application.
I have one issue regarding ios7 which i explain as follows:
i have created one view on UITable Cell Custom Button click like as follows. this view contains UITextView to input the text and fell it on cell later as on comment table.
// For iOS7
UITableView *table = nil;
if([CommonFunction isiOS7]){
table = (UITableView*)[[[[[sender superview] superview] superview] superview] superview];
indexPath=[[NSIndexPath alloc] init];//
indexPath = [table indexPathForCell:self];
}
else {
indexPath=[[NSIndexPath alloc] init];//
indexPath = [(UITableView *)self.superview indexPathForCell: self];
}
NSLog(#"indexPath:%#",indexPath);
if (appDelegate.dictLogedInUserData) {
//[(UITableView*)self.superview scrollToRowAtIndexPath:[(UITableView *)self.superview indexPathForCell:self] atScrollPosition:UITableViewScrollPositionTop animated:YES];
[table scrollToRowAtIndexPath:[table indexPathForCell:self] atScrollPosition:UITableViewScrollPositionTop animated:YES];
TextInputView *txtInput = [[TextInputView alloc] init];
txtInput.delegate=self;
if ([btnAddComment.titleLabel.text length]>0) {
if (![btnAddComment.titleLabel.text isEqualToString:#"Add Comment"]) {
txtInput.txtComments.text = btnAddComment.titleLabel.text;
}
}
txtInput.backgroundColor = [UIColor clearColor];
//[(UITableView*)[self superview] setScrollEnabled:NO];
[table setScrollEnabled:NO];
[self addSubview:txtInput];
[self bringSubviewToFront:txtInput];
}
When use enter some text i need to fill this UITextView text value to my UITableCell CustomView which code is as follows,This is delegate method:
//InputText Delegates
- (void)didFinishInput:(NSString *)inputedText {
btnAddComment.titleLabel.text = inputedText;
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:indexPath, #"indexPath",
[NSString stringWithFormat:#"%#",inputedText],#"commentText",nil];
NSLog(#"dict:%#",dict);
topic *t = [appDelegate.arrayTopicsData objectAtIndex:indexPath.row];
t.user_comment = inputedText;
[appDelegate.arrayTopicsData replaceObjectAtIndex:indexPath.row withObject:t];
[self LoadDataForIndex:indexPath];
[delegate didAddComment:dict];
[(UITableView*)[self superview] setScrollEnabled:YES];
if ([inputedText length]<=0) {
btnAddComment.titleLabel.text = #"Add Comment";
}
}
In the above code it always crash when i enter text on UITextView and press Done button of key board. it gives EXE-BAD-Access on indexPath in iOS7.
Please help me how to get Indexpath on UITextView delegate method when i am passing NSString to the Method.
Please help me.
I am trying to scroll my UITableViewCell up one UITableViewCell position when the selected cell reaches the last position in the UITableView
I have the logic correct for identifying the last visible UItableViewCell of the UITableView. However the code to make the UITableView scroll up one position is not working. This is the code I have written.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
cellSelectedBool = YES;
cell = (CustomFinishingCell *)[tableView cellForRowAtIndexPath:indexPath]; // error
[cell.widthTexField becomeFirstResponder];
// Gets an array of current visible cells in UITableView
NSArray *visibleCells = [tableView visibleCells];
NSLog(#"%#", visibleCells);
// get indexpath of last cell
UITableViewCell *lastCell = [visibleCells lastObject];
NSIndexPath *lastCellIndex = [finishingTableView indexPathForCell:lastCell];
// perform scroll when last visible cell is selected
if (indexPath.row == lastCellIndex.row) {
NSLog(#"BIGGER");
int cellHeight = 44;
[UIView animateWithDuration:.25 animations:^{
[finishingTableView setContentOffset:CGPointMake(finishingTableView.contentOffset.x,finishingTableView.contentOffset.y + cellHeight) animated:YES];
}];
}
you should use:
NSIndexPath *rowToSelect; // assume this exists and is set properly
UITableView *myTableView; // assume this exists
[myTableView selectRowAtIndexPath:rowToSelect animated:YES scrollPosition:UITableViewScrollPositionNone];
[myTableView scrollToRowAtIndexPath:rowToSelect atScrollPosition:UITableViewScrollPositionNone animated:YES];
if (indexPath.row == lastCellIndex.row) {
[finishingTableView scrollToRowAtIndexPath:indexPathatScrollPosition:
UITableViewScrollPositionNone animated:YES];
}
I have a UITableView. I have an image like an arrow beside the tableview.
I have to check whether the arrow is intersecting with a particular cell so that I can programatically select that cell. How do I do this?
I dont have a problem in the type of selection of the cell/row but having problems detecting whether the arrow is on a particular row.
I have written this code but not working :
NSArray *paths = [tableView indexPathsForVisibleRows];
for (NSIndexPath *path in paths)
{
CGRect myRect = [tableView rectForRowAtIndexPath:indexPath];
CGRect myRect1 = CGRectMake(-12, 234, 55, 52);
if (CGRectIntersectsRect(myRect1, myRect))
{
// Also tried [tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
cell.selected = YES;
NSLog(#"\n \n \n myrect = %# myrect1 = %# , index = %# \n \n ", NSStringFromCGRect(myRect),NSStringFromCGRect(myRect1), indexPath);
}
else
{
cell.selected = NO;
}
}
Images shows what I want to achieve and not whats existing.
Ok...for the problem with the arrow... I think the table is returning the rectangle for each row IN REGARDS to the table's frame. So I think you need to do this:
for (NSIndexPath *path in paths)
{
CGRect myRect = [tableView rectForRowAtIndexPath:indexPath];
CGRect myRect1 = CGRectMake(-12, 234, 55, 52);
myRect.origin.x += tableView.frame.origin.x;
myRect.origin.y += tableView.frame.origin.y;
if (CGRectIntersectsRect(myRect1, myRect))
{
//selection code here
}
else
{
// whatever you want
}
}
I have found the answer.. Posting the code
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
NSArray *paths = [tableView indexPathsForVisibleRows];
for (NSIndexPath *indexPath in paths)
{
CGRect myRect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:[tableView superview]];
CGRect myRect1 = CGRectMake(-12, 236, 20, 30);
if (CGRectIntersectsRect(myRect1, myRect) && notSelected == FALSE)
{
notSelected = TRUE;
[tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}
else
{
notSelected = FALSE;
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
}
}