简体   繁体   English

如何将开源Tableview徽章委派给UITableview

[英]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 我知道几次有人问过类似的问题,关于如何将徽章添加到tableviewcell,但我无法使其正常工作

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 所以我尝试了这两个源代码TDbadgcellDDbadgecell

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 现在的问题是我不能委托他们,我必须导入他们的.h类并在我的表视图中调用以下函数之一

- (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. 但是当我这样做时,我的tableView didSelectRowAtIndexPath:(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender方法无法正常工作,我可以单击这些行但它们保持高亮蓝色,但我的右边的箭头也没有任何反应表的消失。

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. 编辑:::放置NSLOG后,我可以看到确实调用了select select行,但是执行segue仍然不起作用。 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 刚刚使用标识符调用了perfromsegue

because this is called - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender also when you call [self performSegueWithIdentifier:@"yoursegue" sender:self]; 因为这被称为- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender当您调用[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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM