jjzjj

ios - TableView Cell Button 更改其他单元格的内容

coder 2024-01-24 原文

单击特定表格 View 单元格上的按钮后,该按钮会单击并变为复选标记。但是,其他单元格也会变为复选标记。重新加载 tableview 不会使复选标记消失。我知道这与可重复使用的电池有关。但是如何从单击单元格按钮时调用的方法更新 cellForRowAtIndex 呢?

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

    // Configure the cell...
    static NSString *ReusableIdentifier = @"Cell";
     SetListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReusableIdentifier forIndexPath:indexPath];


    cell.delegate = self;

    if ([self.selectedRows containsIndex:indexPath.row]) {
    [cell.plusButton setBackgroundImage:checkImage forState:UIControlStateNormal];
        }
        else {
            [cell.plusButton setBackgroundImage:plusImage forState:UIControlStateNormal];
        }


    return cell; 

    }

自定义单元格类中的方法。

- (IBAction)addSongButtonPressed:(UIButton *)sender
{
[self.delegate addSongButtonPressedOnCell:self];

UIImage *checkImage = [UIImage imageNamed:@"check.png"];
[self.plusButton setBackgroundImage:checkImage forState:UIControlStateNormal];

}

来自单元格委托(delegate)的方法。

-(void)addSongButtonPressedOnCell:(id)sender
{
    NSIndexPath *indexPath = [self.searchTableView indexPathForCell:sender];
NSMutableDictionary *track = [self.searchTracks objectAtIndex:indexPath.row];

[self.selectedRows addIndex:indexPath.row];


}

最佳答案

您需要在单元格外部跟踪选择状态 - 单元格是显示数据模型信息的 transient 对象,您不能使用它来存储状态。

由于您的轨道数据有不可变的字典,您将需要添加一个额外的数据结构来存储您的选择状态。我会使用 NSMutableSet

添加一个属性到你的 View Controller

@property (strong,nonatomic) NSMutableSet *selectedRows;

并在 viewDidLoad 中初始化它

self.selectedRows=[NSMutableSet new];

然后您可以使用 to 来跟踪/检查选择状态 -

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

// Configure the cell...
static NSString *ReusableIdentifier = @"Cell";
SetListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ReusableIdentifier forIndexPath:indexPath];

NSDictionary *track = [self.searchTracks objectAtIndex:indexPath.row];
cell.searchSongTitle.text = [track objectForKey:@"title"];
cell.searchArtist.text = [[track objectForKey:@"user"]objectForKey:@"username"];

cell.plusButton.tag=indexPath.row;    

if ([self.selectedRows containsIndex:indexPath.row]) {
    [cell.plusButton setBackgroundImage:checkImage forState:UIControlStateNormal];
}
else {
    [cell.plusButton setBackgroundImage:nil forState:UIControlStateNormal];
}



//If there is no picture available. Adds a Custom picture.
if ([[track objectForKey:@"artwork_url"] isEqual:[NSNull null]]){
    cell.searchAlbumArtImage.image = [UIImage imageNamed:@"SoundCloudLogo"];
}
else{
    //Init the cell image with the track's artwork.
            UIImage *cellImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[track objectForKey:@"artwork_url"]]]];
cell.searchAlbumArtImage.image = cellImage;

            }
return cell; 

}

然后您的按钮按下处理程序变为 -

- (IBAction)cellAddSongButtonPressed:(UIButton *)sender {
    NSInteger index =  sender.tag;
    [self.selectedRows addIndex:index];
    [sender setBackgroundImage:checkImage forState:UIControlStateNormal];
}

创建 Track 类比使用 NSDictionary 会更简洁

关于ios - TableView Cell Button 更改其他单元格的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28120678/

有关ios - TableView Cell Button 更改其他单元格的内容的更多相关文章

  1. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  2. ruby-on-rails - Ruby on Rails 迁移,将表更改为 MyISAM - 2

    如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设

  3. ruby - 如何将脚本文件的末尾读取为数据文件(Perl 或任何其他语言) - 2

    我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚

  4. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  5. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  6. ruby - Capistrano 3 在任务中更改 ssh_options - 2

    我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe

  7. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  8. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  9. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

  10. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

随机推荐