iOS: Customizing TableView cell to mimic music player - ios

Is there an easy way of having a tableview cell like we see here with numbering like this and the border around. Is this created using different sections?

You need to create a custom UITableViewCell.
If you're using storyboards look here:
See this link http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/
If not here is a rundown:
Basically create a new class that inherits from UITableViewCell and a XIB. Drag a UITableViewCell to the XIB and set it to the class that you created previously.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = #"CustomCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
//*- Load your custom XIB. objectAtIndex:0 says load the first item in the XIB. Should be your UITableViewCell that you dragged onto your XIB in Interface Builder.
cell = [[[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil] objectAtIndex:0];
}
//*- Customize the cell, i.e., cell.myLabel.text = #"Text";
return cell;
}
Using this technique you can layout your cell with three labels, one for the number and one for the name of the song and one for the song time. Add a background image view for the border and color.
A simple way to get the song number in the table is to use the indexpath.
cell.myLabel.text = [NSString stringWithFormat:#"%i", indexPath.row + 1];

Related

Scrolling tableview getting duplicate images

Im using NYXProgressiveImageView(https://github.com/Nyx0uf/NYXImagesKit) to load the image into cell progressively
When I’m scrolling getting duplicate images
How can i avoid this
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:#"Customcell"];
if (cell == nil) {
// Load the top-level objects from the custom cell XIB.
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil];
// Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain).
cell = [topLevelObjects objectAtIndex:0];
}
[cell.Imageview1 loadImageAtURL:[NSURL URLWithString:MyURL]];//Here Imageview1 is NYXProgressiveImageView.
}
I think Its due to progressive download,I want to do similar to this http://nghiatran.me/advanced-issues-the-right-way-to-load-content-in-backgrounds-thread-with-tableview/
How to do this this with NYXProgressiveImageView class
Thank you..
You need to subclass UITableViewCell override prepareForReuse and set the imageView.image = nil. The cell's are reused in a table so unless you clean it out before it gets reused it will appear to "duplicate" your data.
You might give DFImageManager a try, it supports progressive image decoding and there are no known bugs. Check out demo project for an example on how to enable and use progressive decoding.

UITableView - reusing cells while in editing mode

I'm finding that if you set a table view into editing mode, upon scrolling the table after deleting a row the cell edit control (the red minus sign on the left) is in a random state (turned vertical or horizontal) on the rest of the cells as you scroll the table. Presumably because I'm reusing cells like this:
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
How can I force the edit control to be in the correct state for each cell? It should always be in the default horizontal state unless I tap it to delete a cell.
EDIT: Here's the cell code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"WhateverIdentifier";
MyCell *cell = nil;
//This IF statement fixes the problem, but then I'm not reusing the cells
if (!tableView.isEditing) {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
}
if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:#"MyCell" owner:self options:nil] objectAtIndex:0];
}
//customize cell
return cell;
}
Are you calling [super prepareForReuse] in the method prepareForReuse of your custom cell?
That resolved my problem.
I just checked in a UITableView I had handy, and I don't see that problem. I'm using dequeueReusableCellWithIdentifier: as you are (without the if statement, of course). Are you doing something special with swiping, or deleting multiple rows or something?
(I'd make this a comment but can't yet. I'll delete it once you've resolved your problem.)

Can't set properties in Custom UITableViewCell

I made a custom table view cell - i have a header, implementation, and nib. In the nib I set the style to custom, dragged a label on it and made an outlet in the nibs file owner.
From my UITableView Controller I have this code:
static NSString *CellIdentifier = #"adbActivityCell";
adbActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
//cell =[[adbActivityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.price.text = [NSString stringWithFormat:#"cell #%d", indexPath.item + 1];
return cell;
If I run this as is XCode tells me that the UITableView Controller is not key value compliant for the label property (the label is named "price"). If I comment out the two lines above and uncomment that one line my application runs, but the label doesn't show up at all, even if I set default text for it.
I've spent quite a lot of time researching tutorials and questions on here with no luck.
Its all about view hierarchy.
You have to add your label outlet to the custom UITableViewCell, because it is the superView of your label in view heirarchy.
That means label is contained in custom cell thats why you have add outlet to custom cell.
self.view->tableView->CustomCellView->UILabel
In your customCell.h file set the IBOutlet of the label. Your problem will be solved.

Xcode - Change label data on UITableViewCell

I have a TableView that i populate with custom cells. In this code i would like to set the data on my different labels, but it doesnt seem to work. I can not add any outlets, not sure why i cant do that. When i try to change the data with cell.textLabel.text = #"Data"; it seems to add a new label instead of changing the text of the current one.
What should i do? Code below shows how i populate the list.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SenasteTableCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
if(cell == nil)
{
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"SenasteTableCell" owner:nil options:nil];
for (UIView *view in views)
{
cell = (SenasteTableCell*)view;
}
}
return cell;
}
Set a unique tag to the label in customcell and you can get its instance as
UILabel *Label=(UILabel *)[cell viewWithTag:2];//2 is th unique tag value i set in the cell for that label
You can get value for every view objects in the cell like this
Eg
UIButton *sampleButton=(UIButton *)[cell viewWithTag:4];
You should be able to create outlets. If you are using a custom table view cell (which Im assuming SenasteTableCell is) make sure that in the interface builder you click on the cell and set its class to SenasteTableCell. At that point you should be able to control drag from your labels to the file and create outlets.
I think you have not set the FileOwner Property form XIB file.
set fileowner as UITableViewClass and UITableViewCell As SenasteTableCell.
To connect outlets you have to first select SenasteTableCell and there will be a list of oulets.

When creating my UITableViewCells, why is the init never called? (Resulting in blank cells?)

In my UITableView, I recently changed the structure of the cell from formerly just putting UILabels in the contentView of the cell, to adding two UIViews (CellFront and CellBack, on top of one another) into the contentView (so I can achieve a sliding effect by sliding the top one off and revealing the lower one) and adding the UILabels to the top UIView.
However, now, for whatever reason, the cells never get init'd and as a result my UITableView is full of blank cells.
My cell gets created as follows (ArticleCell is a subclass of UITableViewCell):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = nil;
ArticleInfo *articleInfo = [self.fetchedResultsController objectAtIndexPath:indexPath];
// Checks if user simply added a body of text (not from a source or URL)
if ([articleInfo.isUserAddedText isEqualToNumber:#(YES)]) {
CellIdentifier = #"BasicArticleCell";
}
else {
CellIdentifier = #"FullArticleCell";
}
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[ArticleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// If the user simply added a body of text, only articlePreview and progress has to be set
cell.preview = articleInfo.preview;
// If it's from a URL or a source, set title and URL as well
if ([articleInfo.isUserAddedText isEqualToNumber:#(NO)]) {
cell.title = articleInfo.title;
cell.URL = articleInfo.url;
}
return cell;
}
But I set a breakpoint on the initWithStyle method above within the if statement and it never gets called:
What would cause this? I'm deleting the app and building it from scratch every time, so data is definitely being added to the UITableView, but all the cells are blank. And I can tell a bunch of cells are being added as I have disclosure indicators on all of them, and the table view just gets filled with empty cells with the indicators only.
What am I doing wrong?
try
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
instead of
ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
the first one is the old standard way. It will not create a cell for you. While with the second a cell will be created form the storyboard. So if you use storyboards you should use indeed the method you are using now, but it will never go info the if branch, as the cell will never be nil.
when instantiating form storyboard, initWithStyle:reuseIdentifier: is never called. Either set everything up in -initWithCoder: or -layoutSubviews

Resources