我知道关于如何将徽章添加到 tableviewcell 的类似问题已被问过几次,但我无法使其正常工作
基本上我想要的是向用户显示一个简单的通知,要么是表格视图单元格右侧的红色数字,要么是一个矩形,或者像本机电子邮件应用程序一样。
所以我尝试了这两个源代码TDbadgcell和DDbadgecell
现在的问题是我不能委托他们,我必须导入他们的 .h 类并在我的表格视图中调用以下任一函数
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
TDBadgedCell *cell = [[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
或者
// 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];
}
但是当我这样做时,我的tableView didSelectRowAtIndexPath:
方法(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
不起作用,我可以单击行但它们保持蓝色,没有任何反应,我在表格右侧的箭头也消失了。
那么如何使用上述源代码或任何其他方法将徽章添加到表格视图单元格行?
编辑::: 放置 NSLOG 后,我可以看到确实调用了选择行,但执行 segue 仍然不起作用。无需添加上述任何代码,它就可以完美运行。
- (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];
}
}