Passing a UILabel value of UItableviewcell to another view in iPhone returning null value? [closed] - xcode4

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am a newbie to objective-C and got strucked at a particular point.I have to pass a UILabel value from tableviewcell to a label in Scrollview when accessoryButtonTappedForRowWithIndexPath action takes place .But the value is not passed ..I donno where Iam going wrong? I am writing this code :
ViewController1.h:
UILabel *name1;
#property(nonatomic,retain)IBOutlet UILabel *name1;
ViewController1.m:
- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
ViewController2 *v2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
v2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:v2 animated:YES];
v2.provName=[name1 retain]; //name1 is the name of UILabel in TableView.
[v2 release];
}
ViewController2.h
UILabel *providerName;
SString *provName;
ViewController2.m:
- (void)viewDidLoad
{
providerName =[[UILabel alloc] init];
[providerName setFrame:CGRectMake(10,10,300,50) ];
providerName.textAlignment=UITextAlignmentLeft;
providerName.backgroundColor=[UIColor blackColor];
self.providerName.text=self.provName;
providerName.highlightedTextColor=[UIColor whiteColor];
[self.view addSubview:providerName];
}
I can see the label but not the value in it ...Y is it so?How to pass a UIlabel value to another view ?

In your accessoryButtonTappedForRowWithIndexPath just make some changes as below,
add
v2.provName=[name1 retain]; //name1 is the name of UILabel in TableView.
just above
[self presentModalViewController:v2 animated:YES];
and also as provName in V2 is synthesized no need to retain just assign it..
Edit: To get the Cell use the below
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
v2.provName = cell.name1;
UITableViewCell can also your custom cell.
Edit: changes in CellForRow
Update your cellForRow as
-(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];
}
NSMutableDictionary *d = (NSMutableDictionary *) [arr objectAtIndex:indexPath.row];
cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
UILabel* name1= [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 320, 10)];
name1.font=[UIFont boldSystemFontOfSize:14];
[name1 setTextAlignment:UITextAlignmentLeft];
[name1 setText:[d valueForKey:#"Name"]];
name1.tag = 111;
[cell addSubview:name1];
[name1 release];
return cell;
}
do not make your cell and name1 as global, use it only in cellForRow
update your didSelectRow as below
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel* name1 = (UILabel*)[cell viewWithTag:111];
v2.provName = name1.text;
this should work fine.

Related

watchkit WKAlertAction openSystemURL

I am trying to show the User different Phone Numbers on Apple Watch and he clicks on one than phone call alert should appear. I'll do it like this but the Alert is just dismissed without call action:
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:0];
WKExtension *myExt = [WKExtension sharedExtension];
for (NSString *phone in arr) {
NSString *tel = [NSString stringWithFormat:#"tel:%#",phone];
WKAlertAction *act = [WKAlertAction actionWithTitle:tel style:WKAlertActionStyleDefault handler:^(void){
[myExt openSystemURL:[NSURL URLWithString:phone1]];
}];
[tempArray addObject:act];
}
NSString *titleMessage = #"Call";
NSString *textMessage = #"Please select the number you want to call.";
NSString *cancel = #"Cancel";
WKAlertAction *act = [WKAlertAction actionWithTitle:cancel style:WKAlertActionStyleDestructive handler:^(void){
}];
[tempArray addObject:act];
[self presentAlertControllerWithTitle:titleMessage message:textMessage preferredStyle:WKAlertControllerStyleAlert actions:tempArray];
Buttons are shown as expected and the Handler is also called with the correct Phone Number. But it does not openSystemURL. Does somebody know why and how to fix? Thanks!
I think you forgot to add "tel" scheme ,Use below code :
[WKAlertAction actionWithTitle:#"tel" style:WKAlertActionStyleDefault handler:^(void){
[[WKExtension sharedExtension]openSystemURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel:%#",#"YOUR NUMBER"]]];
}];
About Apple URL Schemes

Unable to append items in value array of mutable dictionary

NSMutableDictionary *mutableDict = [NSMutableDictionary dictionaryWithDictionary: #{ #"Maruti":#[#"m1",#"m2"], #"Hyundai":#[#"h1",#"h2"] } ];
How can I add values in value array of each key so that my output would look like:
Maruti:[m1,m2,m3] Hyundai:[h1,h2,h3]
Both Maruti and hyundai dictionary are NSDictionary so you cannot add any new values into them , plus you cannot use any of them outside of the scope of this method, but if you still want to do something like this you can go with this.
NSMutableDictionary * tempDict = [[NSMutableDictionary alloc]init];
[tempDict setValue:#"5" forKey:#"number"];
[tempDict setValue:#"6" forKey:#"Hidden"];
NSMutableDictionary *tempDict2 = [[NSMutableDictionary alloc]init];
[tempDict setValue:#"5" forKey:#"number"];
[tempDict setValue:#"6" forKey:#"Hidden"];
NSMutableDictionary *mutableDict=[[NSMutableDictionary alloc] init];
[mutableDict setValue:tempDict forKey:#"Dict 1"];
[mutableDict setValue:tempDict2 forKey:#"Dict 2"];

NSTableCell setDataCell disables editing

I have a NSTableView which I dynamically add columns. I've just added a call to setDataCell to customize my cell. The code looks like:
for(NSUInteger columnIndex = 0; columnIndex < resultSet.columNames.count; ++columnIndex)
{
NSTableColumn * newColumn = [[NSTableColumn alloc] initWithIdentifier: [NSString stringWithFormat: #"%ld", columnIndex]];
[newColumn.headerCell setAlignment: NSCenterTextAlignment];
[newColumn.headerCell setStringValue: resultSet.columNames[columnIndex]];
[newColumn setDataCell: [[HSDisclosureTextFieldCell alloc] init]];
[newColumn setEditable: YES];
[resultsTableView addTableColumn: newColumn];
if(newColumn.width < 60) [newColumn sizeToFit];
} // End of column loop
If I remove the call to setDataCell then I can still double click my entry and edit it.
I have minimized the HSDisclosureTextFieldCell contains the following (no overrides):
#interface HSDisclosureTextFieldCell : NSTextFieldCell
{
}
But I still cannot double click to edit the field anymore.
Any ideas where I'm going wrong?
Turns out even though I am doing:
[newColumn setEditable: YES];
and have:
- (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
implemented, I needed to switch my setDataCell to be:
HSDisclosureTextFieldCell * cell = [[HSDisclosureTextFieldCell alloc] init];
[cell setEditable: YES];
[newColumn setDataCell: cell];
Now that I have done that, everything seems to be working properly.

NSAttributedString Strikethrough does not draw over entire length of text

I am inserting an NSAttributedString into an NSTableView, and depending on it's content I am adding attributes to its style dictionary.
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
NSAttributedString *theValue;
NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:[NSFont systemFontOfSize:11] forKey:NSFontAttributeName];
currentObject = [ticketsDataSource objectAtIndex:rowIndex];
if ([[currentObject valueForKey:aTableColumn.identifier] isKindOfClass:([NSString class])]) {
NSString *currentValue = [currentObject valueForKey:aTableColumn.identifier];
if ([currentValue isEqualToString:#"resolved"]) {
[attributes setObject:[NSColor colorWithCalibratedRed:0.344 green:0.619 blue:0.000 alpha:1.000] forKey:NSForegroundColorAttributeName];
}
if ([previousValue isEqualToString:#"resolved"]) {
[attributes setObject:[NSNumber numberWithInteger:NSUnderlinePatternSolid | NSUnderlineStyleSingle] forKey:NSStrikethroughStyleAttributeName];
}
theValue = [[NSAttributedString alloc] initWithString:currentValue attributes:attributes];
previousValue = currentValue;
}
As you can see, basically what happens is when it writes a string called "resolved" it knows that the very next column, which is the title column, gets a strikethrough. It works just fine, but for whatever reason, the strikethrough is not drawing over the entirety of the text!
Here is an image:
What is going on here?
Yet another weirdness with layer-backing. So I turned it off :(

Trim file extension UITableView

I want to trim the file extension from text I have an NSMutableArray in table cells.
NSMutableArray *theFiles = [NSMutableArray new];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager directoryContentsAtPath:#"/Test"];
for (NSString *s in fileList){
[theFiles addObject:fileList];
}
cell.textLabel.text = theFiles[indexPath.row];
return cell;
This lists for example "Xylophone.m4r" I want to remove the .m4r.
Try -[NSString stringByDeletingPathExtension] (in NSPathUtilities.h).
Actually, for my use, I was able to create a plist programmatically and just not use an extension and it works great! However, anothe rway to do this is:
[string stringByReplacingOccurrencesOfString:#".fileextension" withString:#""];
Just have to delete path extension component:
cell.textLabel.text = [[theFiles objectAtIndex:indexPath.row] stringByDeletingPathExtension];

Resources