简体   繁体   English

重新加载tableview的活动单元格

[英]Reloading Active cells of tableview

I'm developing an application on iOS targeting iOS 7.0 and later. 我正在针对iOS 7.0及更高版本的iOS上开发应用程序。 Now, my question is that how to reload the rows of a UITableView which are visible on screen. 现在,我的问题是如何重新加载在屏幕上可见的UITableView的行。 I'm using dynamic cells. 我正在使用动态单元格。

Actually while selecting an option I'm changing some colors like title color of each cell but those cells which are already loaded on screen are not changed and if I'm loading whole table then its taking a long time. 实际上,在选择一个选项时,我正在更改一些颜色,例如每个单元格的标题颜色,但是已经加载到屏幕上的那些单元格不会更改,如果我正在加载整个表格,则需要花费很长时间。

Waiting for your kind reply. 等待您的友好答复。

Code

//on clicking this button color is updating //点击此按钮时颜色会更新

- (IBAction)btnDone:(id)sender {

appDel.selectedMood=_moodDetView.str;

appDel.barColor=_moodDetView.backgroundColor;

self.navigationController.navigationBar.barTintColor = _moodDetView.backgroundColor;

//[self.mainTblView reloadData];

[self.menuView setBackgroundColor:appDel.barColor];

[_moodDetView setHidden:YES];

} }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath     *)indexPath
  {
    //For menu contents
    if (tableView.tag==2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
            cell.textLabel.textColor = [UIColor whiteColor];
            cell.textLabel.font = [UIFont fontWithName:@"Futura" size:14.0];
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            cell.backgroundColor = [UIColor clearColor];
        }

        switch (indexPath.row) {
            case 0:
            {
                cell.textLabel.text=@"Select Mood";
            }
                break;
            case 1:
            {
                cell.textLabel.text=@"Search by Author";
            }
                break;
            case 2:
            {
               cell.textLabel.text=@"Search by Category";
            }
                break;
            case 3:
            {
                  cell.textLabel.text=@"Favorites";
            }
                break;
            case 4:
            {
                    cell.textLabel.text=@"Feeds";
            }
                break;

            default:
                break;
        }
        return cell;
    }

    //for main table
    else{

    // Configure the cell...

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    FeedCustomCell* CELL=( FeedCustomCell *)cell;

    CELL.cellImageView.layer.cornerRadius=CELL.cellImageView.frame.size.width/2;

    CELL.cellImageView.clipsToBounds=YES;

    CELL.gesRec=[[CustomTapGestureRecognizer alloc]initWithTarget:self action:@selector(authorBtnTouched:)];

    [CELL.lblAuthName setTextAlignment:NSTextAlignmentJustified];

    [CELL.lblAuthName setTextColor:[AppDelegate invertColor:appDel.barColor]];

        NSString * str=[NSString stringWithFormat:@"%@",[[dataArray objectAtIndex:indexPath.row] objectForKey:@"authorId"]];

    UIImage * img=[UIImage imageNamed:str];

    CELL.cellImageView.image=img?img:[UIImage imageNamed:@"n3"];

    [CELL.lblAuthName addGestureRecognizer:CELL.gesRec];

    CELL.gesRec.authorImage=CELL.cellImageView.image;

    NSString * authName=[[dataArray objectAtIndex:indexPath.row] objectForKey:@"authorName"];

    [CELL.lblAuthName setText:authName];

    [CELL.gesRec setAuthorId:(int)str.integerValue];

    [CELL.gesRec setAuthorName:authName];

    [CELL.txtViewQuote setTextColor:appDel.barColor];

    CELL.txtViewQuote.text=[[dataArray objectAtIndex:indexPath.row] objectForKey:@"quoteTxt"];

    CGSize sizeThatShouldFitTheContent = [CELL.txtViewQuote sizeThatFits:CELL.txtViewQuote.frame.size];

    CELL.heightConstraintOfTxtView.constant = sizeThatShouldFitTheContent.height;

    [CELL.contentView sizeToFit];

    return CELL;

    }


}

regards: Syed Meesum Ali 问候:Syed Meesum Ali

Junior Software Developer 初级软件开发人员

If you implement custom cell subclassing UITableViewCell, you can implement prepareForReuse method. 如果实现自定义单元格子类化UITableViewCell,则可以实现prepareForReuse方法。 You can reset some properties example, alpha, selection state in that method. 您可以在该方法中重置一些属性示例,例如alpha,选择状态。

The simplest possible way to do it would be: 最简单的方法是:

[myTable reloadRowsAtIndexPaths:myTable.indexPathsForVisibleRows withRowAnimation:UITableViewRowAnimationAutomatic];

However since you are looking for a more optimized way, The following would be better: 但是,由于您正在寻找一种更优化的方法,因此以下方法会更好:

On button click, in the target method, iterate over visible cells, and make relevant changes in each cell. 单击按钮,在目标方法中,迭代可见的单元格,并在每个单元格中进行相关更改。

- (IBAction)btnDone:(id)sender {

    for (UITableViewCell* cell in yourTable.visibleCells) {

        if ([cell isKindOfClass:[FeedCustomCell  class]]) {

            [((FeedCustomCell*)cell).lblAuthName setTextColor:[AppDelegate invertColor:appDel.barColor]];
        }
    }
}

This should be way faster that reloading (drawing) all visible cells. 这应该是更快的方式是重装(图)所有可见的单元格。

ALSO , a word of advice. 另外 ,一个忠告。 Use different reuseIdentifer s for different types of cells, or your table might cause app crashes. 对不同类型的单元格使用不同的 reuseIdentifer ,否则您的表可能会导致应用程序崩溃。

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

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