ios Noob here. I have an iOS TableView which needs to have a few items in each row: A UILabel for asking a question, Yes button, No button, and a slider to answer "rate between 1 and 5". The slider won't actually show on every question only when the question deals with a rating.
What's the best way to accomplish adding all the "widgets" to the TableView row? Progmatically or through storyboards? Also, if I use storyboards, how would I go about hiding the rating bar and then showing it again when needed?
UPDATED WITH CODE SNIPPET
#property (strong,nonatomic) NSMutableArray *ObjQuestions;
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
OBJ_Question *q1 = [self.ObjQuestions objectAtIndex:indexPath.row];
cell.textLabel.text = q1.QuestionText;
// need to access second and third Label widgets from storyboard here
// as well as show/hide NSSlider
return cell;
}
You have to subclass UITableviewCell and add your custom items to the content view of the UITableViewCell. Use your custometableview cell in your UITableView datasource & delegate method. If you need more info than this, let me know and I can give you a sample code
If you use Storyboards you can create two types of cells, one with the slider, and one without. All you need to do then is to load the correct cell depending on your data for that row.
It's easier than trying to hide and show the slider.
Related
Hello guys i think almost everyone who is in ios development may come across the issue of reuse of the UITableCell by using following code line.
RZRestaurantListingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
I have search lot about this but not getting any desire answer so please help me out in this case.
I have the same issue as most of iPhone developer having after reusing the cell.
I have the UIProgressView inside my cell and one button is there for downloading the video and i am showing the progress there in progress view how much is left.
So Now what i have problem is when i have more data and going out of the screen at that time i am press the download button on very first row of the UITableviewCell then i am scrolling down so the progress also shown in bottom random one cell so the UI changes in two cell rather then one.
You need to implement -prepareForReuse method in your custom cell class and set all cell properties to default value.
- (void)prepareForReuse
If a UITableViewCell object is reusable—that is, it has a reuse
identifier—this method is invoked just before the object is returned
from the UITableView method dequeueReusableCellWithIdentifier:. For
performance reasons, you should only reset attributes of the cell that
are not related to content, for example, alpha, editing, and selection
state. The table view's delegate in tableView:cellForRowAtIndexPath:
should always reset all content when reusing a cell. If the cell
object does not have an associated reuse identifier, this method is
not called. If you override this method, you must be sure to invoke
the superclass implementation.
Refer here for more, https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewCell_Class/#//apple_ref/occ/instm/UITableViewCell/prepareForReuse
You need to assign a progress value inside the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
RZRestaurantListingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// If the cell is reused, the `-prepareForReuse:` of `UITableViewCell` will be called.
//!! Assign current progress value to this cell, otherwise, the progressBar.value may look like a random value.
//!! Because the current cell is reused from a disappeared cell.
cell.progressBar.value = ... ;
return cell;
}
The design may be complex, because the progress may be updated continuously when the cell is on the screen.
Use prepareforreuse method to clear content of cell before using it... e.g.
-(void)prepareForReuse
{
[super prepareForReuse];
self.textLabel.text = #"";
self.detailTextLabel.text = #"";
self.imageView.image = nil;
}
I am trying to implement rating stars by using kDRATING VIEW .i have used following code in cellForRowAtIndexpath method but it causes my app to become slower.
If it try to allocate and initialise this in viewdidload method then it when i scroll up and down ,the stars fluctuates . please help in this regard
self.rating = [[KDRatingView alloc] initWithFrame:CGRectMake(0, 0, 60,20)];
[self.rating rateKDRatingView:2.80 outOf:3.0];
[cell.rating addSubview:self.rating ];
return cell;
It sounds like you need to look into UITableViewCell reuse because when you scroll a UITableViewCell out of the screen it will call cellForRowAtIndexPath again to remake this cell when it is back in view and that can cause flickering and memory consumption.
You are adding the KDRatingView to the rating view on the cell so I guess you have a custom cell, so why not instead have the KDRatingView inside the custom cell and just update its value when you need to.
Try this solution with some cell reuse:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"RatingCell";
RatingCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
// initialisation code
cell = [RatingCell new];
}
// setting code
[cell setRatingViewValue:2.8 outOf:3.0];
}
That way it only creates the cell once, so it won't slow down your app. Then each time the cell would have been recreated it just updates the cells rating (and anything else you need to set) instead.
Then you just need to implement the setRatingViewValue:outOf: method in your custom cell to update the KDRatingView that you need to have added and positioned in your custom cell upon initialisation.
In this project I have absolutely no code. In my main.storyboard I have a TableViewController with so far 1 cell. In that one cell I put an Image View in, then I selected the certain picture. The cell is 320 (Width) by 44 (Height). Everything seems fine in the main.storyboard, however I press run, the app opens but there is nothing there, only a bunch of lines.
Any help would be great I will give screenshots to anyone who needs them in order to help me solve this problem. I am using xCode 5.1
If you don't want to write any code than you must tableView type to static!
Check that.
Here's the snippet for TableViewDelegate. Hope this will help. Don't forget to thumbs up
Make sure you have this in your TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1; ---->> should return 1
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10; ----->> return how many rows you want to show, mostly you write
return yourarray.count;
}
I think the problem will be in the method below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = nil; ---->> Instead of UITableViewCell you need to
change to the class name of the
TableCell ( This is where your cell is
created)
cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:#"Cell"]; ---->>Instead of UITableViewCell you need to
change to the class name of the
TableCell ( This is where your cell is created)
}
cell.textLabel.text = #"Cell";
return cell;
}
You have to use either a static tableview that uses predefined cells or (by default) use a dynamic tableview - in that case you have to provide the cells in code by adhering to the tableviews data source protocol. Which you seem to not be doing now.
So, in interface builder, set the tableview to be static and you can build the cells in interface builder and they will be displayed. Make sure you delete the boilerplatecode in table viewcontroller that is given to you to easily implement the protocol (if you don't delete it, the default code returns nil off the cells and so none will get displayed).
When you get it working, find a nice tableview tutorial. I suggest ray wenderlich or the cs193p course on itunesU.
I used a tutorial online to create a slide out side bar (similar to the facebook app).
It's based on using a table view as the side bar.
I cannot change the size (row height) of an individual cell as the table is set to dynamic (as specified by the tutorial. I change the row height in the IB and it makes no difference when running. It turns out I need a static table to use this.
When I change the table to static, it breaks my app with the following code causing the problem. I know a dynamic table is when you want to reuse cells and therefore dequeueReusableCell will cause a problem.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
I don't have much experience with TableViews past this specific tutorial so just need a little bit of help amending the code so I can use a static table.
Thanks
If your table view isn't static, don't make it static to handle the row height. Instead, implement the table view delegate method tableView:heightForRowAtIndexPath: and return the appropriate height for each row.
Setting the height in the XIB / storyboard can have an effect but it depends on the overall configuration as to exactly what (if anything) it will do.
I have a UITableView that I want to alter some of the static cells after I do other processing. I have outlets set up for the cells that I want to modify, but when I look at them using NSLog, they show nil, which indicates to me that I don't have the correct cell. For instance, in the image below I want to add the start time to the label just like I did for Date (date was done when creating the cells for which I got the current date),
I tap on the disclosure indicator which takes me to another scene (this was created in Storyboard, using segues to get from one scene to another) where I get the two times I need. I then return to the main scene (shown) and try to alter the Start Time label, but nothing happens. A NSLog of the label prior to trying to alter it returns this:
oStartTimeCell.textLabel.text: (null)
I have read in one of the Apple docs that this textfield is read-only. If that is true in this case, is there a way I can reload the cells with the updated information? Or is there another way to do this?
You're using the wrong approach. You should not create a reference to a cell using an outlet. Once the cell moves out of the visible view, the outlet will either be null or contain garbage data. Even if (in your situation) the cell will never move out of view, I think it shows you're trying to use a UITableView in a way that was not meant to be.
Instead put the data you want to display in your cells in a dataSource, e.g. an array.
The tableView should use the dataSource to configure the values displayed in the textLabels of the cells. Once you want to update the text displayed in the cells, change the values in the dataSource and call reloadData on the tableView to force the tableView to call -tableView:cellForRowAtIndexPath: and related UITableViewDataSource methods.
Try to create an IBOutlet for each cell and connect it:
IBOutlet UITableViewCell *cell1;
IBOutlet UITableViewCell *cell2;
IBOutlet UITableViewCell *cell3;
And also change your method to:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(indexPath.row == 0) return cell1;
if(indexPath.row == 1) return cell2;
if(indexPath.row == 2) return cell3;
if (cell == nil) {
//create cell;
}
return cell;
}
Are you using a UILabel to display the text ? . If you are just create an outlet to the UIlabel and update it any method like cellForRwoAtIndexPath or didSelectRowAtIndexPath etc that is called after you tableView is loaded.
If you are not using a UILabel and just using cell.textLabel you could do something like
cell.textLabel.text = #"ChangedText" in cellForRowAtIndexPathMethod. Make sure you are editing the required cell by checking indexPath.row
Do [tableView reloadData] to call cellForRowAtIndexPath.