简体   繁体   English

调用UITable View委托方法

[英]Calling UITable View Delegate Method

I'm working with UITableView in UIViewController..I am using the following delegate methods..... 我正在使用UIViewController中的UITableView。我正在使用以下委托方法。

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

Once I load the view I find all delegates has been called..... But I need to call these methods in a button click..... 加载视图后,我发现所有代表都已被调用.....但是我需要在单击按钮时调用这些方法.....
I tried in this way.... 我尝试过这种方式。

[self tableView:table numberOfRowsInSection:rows];
[self tableView:table cellForRowAtIndexPath:path];

I find that these methods has been called,but nothing happened (code inside these delegates doesn't works).... I don't knw why??? 我发现这些方法已被调用,但是什么也没发生(这些委托中的代码不起作用)...。我不知道为什么??? Any suggestions??? 有什么建议么???

Write the code inside the button click function it will call all the delegates of the tableview 在按钮单击功能内编写代码,它将调用tableview的所有委托

[self.tableView reloadData];

or complete code to your solution is:- 或完整的代码到您的解决方案是:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @"Cell";

    customCell *cell = [[customCell alloc]init];
    if (cell == nil) {
        cell = [[[customCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;


    NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"customCell" owner:self options:nil];
    cell = (customCell *)[topLevelObjects objectAtIndex:0];


    int count=0;
    for(id currentObject in topLevelObjects)
    {
        count++;
        if([currentObject isKindOfClass:[customCell class]])
        {

            cell= (customCell *)currentObject;  

            UILabel *sName = (UILabel *)[cell.contentView viewWithTag:1]; 

            //Checking the flag that is set in the btnClicked event
            if (flag) {
                    sName.hidden=YES;
                }

                else{
                sName.text = [arrayResult objectAtIndex:indexPath.row];             
                }

        }
    }

    return cell;
    [topLevelObjects release];
    [cell release]; 

}

-(IBAction) btnClicked:(id) sender{
flag=YES;
[self.tableView reloadData];
}

Try this it will work.. 试试这个,它将起作用..

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

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