【问题标题】:How to change the content of the UITableView with a button click?如何通过单击按钮更改 UITableView 的内容?
【发布时间】:2011-12-23 03:43:16
【问题描述】:

我有一个 UItableview,里面有内容数组。有两个数组,我想通过单击按钮来切换数组。 我的代码是

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

tab.backgroundColor = [UIColor clearColor];
tab.separatorColor = [UIColor clearColor];
cell.chapterAndVerse.text = [NSString stringWithFormat:@"%d",indexPath.row+1];
cell.chapterAndVerse.font = [UIFont fontWithName:@"Georgia" size:17.0];
cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
cell.textLabel.text =  [NSString stringWithFormat:@"  %@",[delegate.allSelectedVerseMalayalam objectAtIndex:indexPath.row]];

cell.textLabel.font = [UIFont fontWithName:@"testfont" size:18];
cell.backgroundColor = [UIColor clearColor];
}

我的数组是delegate.allSelectedVerseMalayalam,我希望在单击按钮时使用delegate.allSelectedVerseEnglish更改此数组。当用户单击英语按钮时,它会将tableviewcontent(我的表名是tab)更改为delegate.allSelectedVerseEnglish .这怎么可能。请帮我解决这个问题。 提前致谢。 编辑:

 - (void)viewDidLoad {
NSLog(@"%@", delegate.allSelectedVerseMalayalam);
    tempArray = [[NSMutableArray alloc] init];
    [tempArray addObjectsFromArray:delegate.allSelectedVerseMalayalam];
    NSLog(@"%@", tempArray);

    [self.tab reloadData];
}
-(IBAction)_clickbtndefaultlnguageset:(id)sender
{
    [tempArray removeAllObjects];
    [tempArray addObjectsFromArray:delegate.allSelectedVerseHindi];
    [self.tab reloadData];

}

在表格视图中

cell.chapterAndVerse.text = [NSString stringWithFormat:@"%d",indexPath.row+1];
cell.chapterAndVerse.font = [UIFont fontWithName:@"Georgia" size:17.0];
cell.chapterAndVerse.frame=CGRectMake(0, 10, 30.0, 20.0);
cell.textLabel.text =  [NSString stringWithFormat:@"  %@",[tempArray objectAtIndex:indexPath.row]];

// cell.textLabel.textColor = [UIColor darkGrayColor];
cell.textLabel.font = [UIFont fontWithName:@"testfont" size:18];
cell.backgroundColor = [UIColor clearColor];

【问题讨论】:

    标签: iphone arrays uitableview event-handling uibutton


    【解决方案1】:

    做一件事需要一个额外的数组并分配它 在源文件中定义 .h 文件

    NSMutableArray *tempArray;
    
    -(void)viewDidLoad{  
    //NSLog(@"%@", your Array);  
    tempArray = [[NSMutableArray alloc] init];  
    [tempArray addObjectsFromArray:yourfirstarray];  
    NSLog(@"%@", tempArray);  
    [self.tableview reloadData];  
    }
    

    按下按钮后

    -(void)pressedBtn{  
    //NSLog(@"%@", your Array);
    [tempArray removeAllObjects];  
    [tempArray addObjectsFromArray:yoursecondArray];  
    [self.tableView reloadData];  
    }
    

    将此临时数组用于表数组

    【讨论】:

    • 在源文件中定义数组。并首先检查您的委托数组是否存在?在 viewDidLoad 中打印数组
    • 您检查过阵列是否存在吗?你的代码中是否有 NSLog 以在控制台中打印数组?
    • 您在控制台中看到了什么?你看到你的阵列被打印了吗?你有没有检查过你的表视图的所有委托方法,比如 numberOfRows ....?
    • see 表示您的数组未分配。它是空的。我认为您必须在 AppDelegate 文件中分配您的委托数组。尝试在 appDelegate 文件中对您的 appdelegate.allSelectedVerseMalayalam 进行 NSLog 记录,这样您就可以了解您的主数组是否已分配。回复
    • 如果您仍然遇到问题,请使用您的 appdelegate 文件代码发布您的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 2018-11-27
    相关资源
    最近更新 更多