0

I am new to iOS i am working on uitableview cell. I want to implement uitableview in a following manner. Expected Result

This is one section. Suppose array has 3 entries than 3 similar sections should appear.

I am storing data using NSUserDefaults. So all data is first fetched into NSMUtableArray. Data in an array is stored as, Suppose UserInfo is my array:

["raj","test1","test2","Mon"], ---- 1st row

["tiya","test3","test2","Tues"], ----- 2nd row

["niha","test1","test5","Wens"] ------- 3rd row

How can I implement above using uitableTable view

  • I suggest that you create a prototype cell that contains all the fields in it instead of a section having separate rows for test, name and day. – Adeel Miraj Dec 28 '15 at 12:43
  • I want a separate row because, on click of each row it should open edit view For ex. if user taps on 1st row than Edit view to edit user's name will open If second row is tapped than test1 edit view opens – user5247057 Dec 28 '15 at 12:55
  • There is an easier way to do that. When user taps on a cell, take the user to a new view controller where he can edit the info contained in any specific cell. – Adeel Miraj Dec 28 '15 at 13:01
  • In my opinion that would be more convenient and appropriate way of doing it. – Adeel Miraj Dec 28 '15 at 13:02
  • So You mean to say, instead of 4 prototype cell, Should have only one prototype cell with all the options – user5247057 Dec 28 '15 at 13:04
  • Yes exactly that's my point. Create a cell in which you have all the required UI components. Place them however you want. Just make sure that the data source of your table view is in accordance with your layout i.e. for each cell you have a dictionary that contains the information that you want to show in each cell. – Adeel Miraj Dec 28 '15 at 13:08
  • Can you provide me some link/ tutorial which implements similar thing, so that i can refer it – user5247057 Dec 28 '15 at 13:12
  • I hope that you have the basic knowledge of using table views. [Here's](http://www.appcoda.com/ios-programming-customize-uitableview-storyboard/) a link to an easy tutorial to get acquainted with the concept of using custom cells in table views. – Adeel Miraj Dec 28 '15 at 13:19
  • OK Thanks. I will try it out. – user5247057 Dec 28 '15 at 13:22
  • Do leave a comment when you are done or have any further questions. I'll post an answer to the question afterwords. – Adeel Miraj Dec 28 '15 at 13:37
  • using Customize UITableview cell, I got expected result. Now Next thing i want to implement is on tap of an element it should allow user to edit the data. So should I implement tap gesture for each element in uitableview custom cell ya is their any other way to implement it. For ex if user taps on name than name edit view should open. There is a separate edit view for each row – user5247057 Dec 29 '15 at 05:55
  • There is no need to use gestures for that. Create a new view controller where the user can edit all the fields of the selected cell. That is easy to do. Also tell me something about the data source of your table view. – Adeel Miraj Dec 29 '15 at 07:56
  • Ya i know that, but its a client requirement. He wants separate edit view for each row. – user5247057 Dec 29 '15 at 08:14
  • I see. Then create another prototype cell that contains the text fields and other UI elements if any. When the user taps on a cell replace that cell with the editable cell. That would do for you I guess. – Adeel Miraj Dec 29 '15 at 08:21
  • Client requirements are: On click of Name--different view appears (navigation controller) wherein user can edit his name only. so what I was thinking to do is In didSelectRowAtIndexPath method I will get rowindex and if I get element tag number than for ex. usr name has tag 101 if(tag == 101) programitically I can call pushViewController – user5247057 Dec 29 '15 at 09:09
  • I'm not getting your point. Pushing a new view controller to edit a cell's info is what I proposed you first. But you wanted to do that on the same screen. Now if you are ready to push a new view controller then you can get the data of the selected row via its indexPath. For that you'll have to set the row number of a cell as tag of all the elements contained in that cell. – Adeel Miraj Dec 29 '15 at 09:37
  • I dint get this "For that you'll have to set the row number of a cell as tag of all the elements contained in that cell." – user5247057 Dec 29 '15 at 09:50
  • ya I want to push a new view controller but separate for each element Name will have separate editview controller Test1 will have separate one so on.... how to get element tag no in didSelectRowAtIndexPath method? – user5247057 Dec 29 '15 at 09:52
  • Ok, I see. You were right about using gesture recognisers or IBActions to be able to know which particular element was tapped. But in the IBAction or the gesture recogniser's selector you'll not have the index path. So you should set the tag of each UI element of every cell equal to the `indexPath.row` when you return the cell in `cellForRowAtIndexPath:` method. Have a look at this [thread](http://stackoverflow.com/questions/2863940/how-can-i-keep-track-of-the-index-path-of-a-button-in-a-table-view-cell) for explanation. – Adeel Miraj Dec 29 '15 at 10:18

3 Answers3

0
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
    // Return the number of sections.
     return Array.count;
   }

   - (NSInteger)tableView:(UITableView *)tableView   numberOfRowsInSection:(NSInteger)section
     {
          // Return the number of rows in the section.
        return 1;
      }


     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
       {


     static NSString *CellIdentifier = @"cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   UILabel *name = (UILabel*)[cell.contentView viewWithTag:1];
   UILabel *test1 = (UILabel*)[cell.contentView viewWithTag:2];
   UILabel *test2 = (UILabel*)[cell.contentView viewWithTag:3];
   UILabel *day = (UILabel*)[cell.contentView viewWithTag:4];  
     name.text = @"raj";
     test1.text = @"test1";
     test1.text = @"test2";
     day.text = @"Mon";
     return cell;



   }
pradip kikani
  • 627
  • 6
  • 14
  • what is cell.name? Is it an IBOutlet of Label If so, how to connect it to IBOutlet because if I try to connect than I get following error: The lblName outlet from the TableViewController to the UILabel is invalid. Outlets cannot be connected to repeating content. – user5247057 Dec 28 '15 at 12:49
0
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {
    retune 1;
   }
    - (NSInteger)tableView:(UITableView *)tableView   numberOfRowsInSection:   (NSInteger)section
 {
           // Return the number of rows in the section.
      return [your array count];
     }

- (UITableViewCell *)tableView:(UITableView *)tableView 
   cellForRowAtIndexPath:(NSIndexPath *)indexPath
         {

      static NSString *CellIdentifier = @"cell";
      UITableViewCell *cell = [tableView   dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) { 
   cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero   reuseIdentifier:CellIdentifier]];
}   

   // alloc label for different rows
     if(indexpath.row==0)
     cell.lbl.text = @"raj";
    cell.lbltest1.text = @"test1";
    cell.lbltest2.text = @"test2";
     cell.lblday.text = @"Mon";
   }

   if(indexpath.row==1)
 {
     cell.lbl.text = @"tiya";
     cell.lbltest1.text = @"test3";
     cell.lbltest1.text = @"test2";
     cell.lblday.text = @"Tues";

}

else if(indexpath.row==2)
 {
 cell.lbl.text = @"niha";
 cell.lbltest1.text = @"test1";
 cell.lbltest1.text = @"test5";
 cell.lblday.text = @"Wens";
}
 return cell;

}

Lalit kumar
  • 1,797
  • 1
  • 8
  • 14
0

Here's little hint:

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
      {
         return [yourArray count];
      }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
       {
            for(int i=0; i<[yourArray count];i++)
       {

        if (indexPath.section == i)
        {
                titleForCell=[yourArray objectAtIndex:i]//test i
                descriptionForCell=[yourArray objectAtIndex:i];// time i
        }
            cell.textLabel.text=titleForCell;
            cell.detailTextLabel.text=descriptionForCell;
       }
    return cell;
    }
Arpit Dongre
  • 1,683
  • 19
  • 30