table view use customized cell file and how ot use segue - objective-c

I user Master-Detail created a project. On the MasterViewController, the table cell i use a MasterCell.h and MasterCell.m to build my customized cell, so my code is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
MasterCell *cell = (MasterCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[MasterCell alloc] initWithFrame:CGRectZero];
NSString *value = [[myContacts objectAtIndex:indexPath.row] valueForKey:#"Name"];
cell.nameContentLabel.text = [NSString stringWithFormat:#"%#", value];
value = [[myContacts objectAtIndex:indexPath.row] valueForKey:#"Tele"];
cell.teleContentLabel.text=[NSString stringWithFormat:#"%#", value];
value = [[myContacts objectAtIndex:indexPath.row] valueForKey:#"Image"];
cell.myImageView.image = [[UIImage alloc] initWithContentsOfFile:value];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
And whan i want click the cell and push the DetailViewController, so my stroyboard is use the origin creat segue, but it didn't work, i think the segue is not my MasterCell, and i had try to change the storyboard cell custom Class, and it was't not success.How do I do? Thanks.

In your storyboard you should create segue not from cell, but from entire UITableViewController and set some identifier, for example "MySegue" and style is "Push".
Then in your tableViewController you should add
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"MySegue" sender:self];
}
and
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"MySegue"]){
//do your stuff
}
}

Related

How to use a subclassed cell from the IB without reusing it again?

I've created a cell with 2 UIButtons in the IB and subclassed it. How can I use it without reusing it over again? (for a small fixed table)
I tried doing something like:
RaffleResultCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
but that won't show my cell in the UITableView, just a blank one.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Where we configure the cell in each row
id currentRaffle = [_winnings objectAtIndex:indexPath.row];
RaffleResultCell *cell = [tableView dequeueReusableCellWithIdentifier:#"raffleResCell"];
if (cell == nil) {
cell = [[RaffleResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"raffleResCell"];
}
return cell;
}
It is not a good practice to avoid Reusability,I will say dont do it
Reusability is done in this line
RaffleResultCell *cell = [tableView dequeueReusableCellWithIdentifier:#"raffleResCell"];
Remove that line and just call alloc method everytime without the check loop
Like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
id currentRaffle = [_winnings objectAtIndex:indexPath.row];
cell = [[RaffleResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"raffleResCell"];
return cell;
}
You told you set up the UITableViewCell in IB then you need to get Nib file and then use that file as
// Get nib file from mainBundle
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RaffleResultCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
RaffleResultCell *cell = (RaffleResultCell *)currentObject;
break;
}
}
now setup any text for your button and return cell

How to delegate an open source tableview badge to UITableview

I know similar questions have been asked few times about how to add badge to tableviewcell, but I could not make it working
Basically what I want is to show user a simple notification either a red number at the right part of the table view cell or a rectangle or like native email app.
So I have tried both of this two source code TDbadgcell and DDbadgecell
Now the problem is I can not delegate them, I have to import their .h classes and call either one of the below functions in my table view
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
TDBadgedCell *cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
or
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
DDBadgeViewCell *cell = (DDBadgeViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DDBadgeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
But when I do that my tableView didSelectRowAtIndexPath: and (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender methods are not working, I can click the rows but they stay higlihted blue nothing happens also my arrow at the right side of the table is dissappears.
So how can I achieve to add a badge to table view cell row either with above source codes or any other methods?
EDIT:::
After putting NSLOG I can see that did select row is called but perform segue still does not work. Without adding any of the above code it works perfect.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//static NSString *CellIdentifier = #"MeetingCell";
static NSString *CellIdentifier = #"Cell";
DDBadgeViewCell *cell = (DDBadgeViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DDBadgeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSString *description =#":";
NSString *name =#"";
NSString *fileStatus=#"";
name = [[self agenda] getFileNameWithSection:[indexPath section] Row:[indexPath row]];
description = [[self agenda] getFileDescriptionWithSection:[indexPath section] Row:[indexPath row]];
fileStatus = [[self agenda] getFileStatusWithFileName:name];
NSString * cellLabel = [NSString stringWithFormat:#" %# : %#",description,name];
//alloc row images
UIImage *docImage = [UIImage imageNamed:#"ICON - Word#2x.png"];
UIImage *xlsImage = [UIImage imageNamed:#"ICON - Excel#2x.png"];
// UIImage *picImage = [UIImage imageNamed:#"ICON - Image#2x.png"];
UIImage *pdfImage = [UIImage imageNamed:#"pdf icon#2x copy.png"];
UIImage *pptImage = [UIImage imageNamed:#"ICON - PPT#2x.png"];
//Determine what status to display for a file
//No need to that since wee use push notification
if ([fileStatus isEqualToString:#"new"]){
cellLabel = [NSString stringWithFormat:#"%# (%#)",cellLabel,#"New"];
cell.badgeText = [NSString stringWithFormat:#"Update"];
cell.badgeColor = [UIColor orangeColor];
}else if ([fileStatus isEqualToString:#"outdated"]){
cellLabel = [NSString stringWithFormat:#"%# (%#)",cellLabel,#"Outdated"];
cell.badgeText = [NSString stringWithFormat:#"Update"];
cell.badgeColor = [UIColor orangeColor];
}else if ([fileStatus isEqualToString:#"updated"]){
cellLabel = [NSString stringWithFormat:#"%# (%#)",cellLabel,#"Latest"];
}
UIFont *font1 = [UIFont fontWithName:#"Century Gothic" size:15.0f];
cell.textLabel.font=font1;
//if there is no file user can not tocuh the row
if ([name length]==0) {
cell.userInteractionEnabled = NO;
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = description;
}else{
//set cell title
cell.textLabel.text = cellLabel;
}
//set row images
if ([name rangeOfString:#"docx"].location != NSNotFound) {
cell.imageView.image= docImage;
}else if ([name rangeOfString:#"xlsx"].location != NSNotFound){
cell.imageView.image= xlsImage;
}
else if ([name rangeOfString:#"pdf"].location != NSNotFound){
cell.imageView.image= pdfImage;
}
else if ([name rangeOfString:#"ppt"].location != NSNotFound){
cell.imageView.image= pptImage;
}
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rounded corner box center#2x.png"]];
// At end of function, right before return cell
cell.textLabel.backgroundColor = [UIColor clearColor];
NSLog(#"%#",cell.textLabel.text);
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];.0
*/
NSLog(#"didselect row is called %d",indexPath.row);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[DBSession sharedSession] isLinked]) {
if([[segue identifier] isEqualToString:#"pushDocumentView"])
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSInteger section =[indexPath section];
NSInteger row = [indexPath row];
NSString *fileName = [[self agenda] getFileNameWithSection:section Row:row];
NSDictionary * agendaDic = [[[self agenda]revision] objectForKey:fileName];
NSString *fileStatus =[agendaDic objectForKey:#"status"];
DocumentViewController *docViewController = [segue destinationViewController];
//This will display on the Document Viewer
docViewController.fileName=fileName;
//This will determine remote or local copy display
docViewController.fileStatus=fileStatus;
}
}else {
[self displayError];
[self setWorking:NO];
}
}
Just called a perfromseguewith Identifier
because this is called - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender also when you call [self performSegueWithIdentifier:#"yoursegue" sender:self];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];.0
*/
[self performSegueWithIdentifier:#"yoursegue" sender:self];
NSLog(#"didselect row is called %d",indexPath.row);
}

How to directly edit a row in a UITableView, without entering in Editing Mode

In a UITableView, I would like to be able to edit the cell.textLabel.text property of the row, when such row is touched.
In other terms, I would like to be able to edit the row directly touching it, instead of entering into edit mode.
How can I implement this?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CMTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
int tagsCount = [[currentComic Tags] count];
[[cell textField] setTag:[indexPath row]];
[[cell textField] setText:[tagsDisplayName objectAtIndex:[indexPath row]]];
return cell;
}
And this is the subclass CMTableViewCell:
...
-(void)viewDidLoad
{
textField = [[UITextField alloc] init];
[textField setFrame:CGRectMake(5,5,50,400)];
[self addSubview:textField];
}
Add UITextField without borders as subview. Before adding it to subview - set Tag nubmer to UITextField from indexPath.row at tableview cellForRowAtIndexPath method. When user entered data - save it with UITextFieldDelegate methods when "Return" button was pressed. Unfortunately I can't give you code, because right now I'm on Windows. Home this will help
UPDATE: Tag number needed to change data in your DataSource. When you pressed "Return" button in your UITextField, you can save changed by getting UITableViewCell by this tag number from UITextField.
It's simple:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = #"...";
}
UPDATE
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITextField *textField = [[UITextField alloc] init];
[textField setTag:[indexPath row]];
[tagsTableView addSubview:textField];
FreeAndNil(textField);
}

connect uitableviewcells to diffrent viws via segue

I create a UITableView programmatically with different cells and sections that connects to the other views in storyboard like below picture:
My question is:
how can I connect these 3 cells to these different views ,would you please helping me
Thanks in advance!
Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]];
NSArray *contents = [[self sectionContents] objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
[[cell textLabel] setText:contentForThisRow];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedIndex = indexPath.row;
[self.tableView reloadData];
}
Edit:
When I use this method It just load the WorkTime view even if user select the absence cell,
Does any one knows why ?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_selectedIndex = indexPath.row;
[self.tableView reloadData];
//maybe you could use a switch/case here, to assign the correct string to the segue identifier.
switch (indexPath.row){
case 0:
[self performSegueWithIdentifier:#"test" sender:self];
break;
case 1:
[self performSegueWithIdentifier:#"set" sender:self];
break;
case 2:
[self performSegueWithIdentifier:#"get" sender:self];
break;
}
}
Your last method is the way you want to go, but by the looks of it, you need to be checking indexPath.section not indexPath.row

TableView: didSelectRowAtIndexPath, How to catch a row name

I fill my tableView with random data. So i don't know how many row i have and how to redirect it to screen i want to. I filled:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyClass *currentScreenElement = [self.tableRows objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:currentScreenElement.objectName];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:currentScreenElement.objectName];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = currentScreenElement.objectName;
return cell;
}
And this works fine. I have a tableView with filled rows. Now i want to redirect to new screen and I doing it with:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NewScreenClass *myScreen = [[NewScreenClass alloc] init];
self.detailViewController = [[CreateView alloc] initWithScreenDef:[myScreen returnScreenFromName:#"second"]];
[self.navigationController pushViewController:self.detailViewController animated:YES];
}
Of course i know that what I actualy do here is create the same screen for every row. But how can i redirect to screen i want to?
My currentScreenElement object contains properties: objectName (display name in tableView) and objectTarget (screen definition). Both NSString.
And my question is should i save a my currentScreenElement to row patch somehow or maybe I should catch a objectName from dislay name?
In didSelectRowAtIndexPath you can get the cell by using
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
Then just get the text from the cell.
cell.textLabel.text
Why can't you use the self.tableRows array again?
MyClass *currentScreenElement = [self.tableRows objectAtIndex:indexPath.row];
NSString *title = [currentScreenElement objectName];
It's the same thing you did to setup that row, just use it again.