Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何根据截面在每行中显示不同的图像?_Iphone_Objective C_Uitableview - Fatal编程技术网

Iphone 如何根据截面在每行中显示不同的图像?

Iphone 如何根据截面在每行中显示不同的图像?,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我有一个UITableview,表中有许多部分。现在我必须为不同部分的行设置不同的图像。我知道如何为分区中的不同行设置不同的图像,但在这里我必须在不同分区的行上设置不同的图像 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(indexPath.section == yourSection) { if(indexPath.row ==

我有一个
UITableview
,表中有许多部分。现在我必须为不同部分的行设置不同的图像。我知道如何为分区中的不同行设置不同的图像,但在这里我必须在不同分区的行上设置不同的图像

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == yourSection) {
     if(indexPath.row == yourRow) {
     // Do something
    }
  }
}
使用此基本逻辑可根据您的需要进行自定义。如果数字较大,最好使用
switch case
而不是
if else

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    NSArray *listData =[self.tableContents objectForKey:[self.sotreKeys objectAtIndex:[indexPath section]]];


    UITableViewCell * cell = [tableView 
                              dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

    if(cell == nil) {

        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleValue1 
                 reuseIdentifier:SimpleTableIdentifier] autorelease];


    }
    if(indexPath.section == 0 && indexPath.row ==0)
    {
        /*Add image on section of cell*/
cell.imageView.image = [UIImage imageNamed:@"image.png"];
    }
if(indexPath.section == 0 && indexPath.row ==1)
    {
        /*Add image on section of cell*/
cell.imageView.image = [UIImage imageNamed:@"image.png"];
    }
if(indexPath.section == 1 && indexPath.row ==1)
    {
        /*Add image on section of cell*/
cell.imageView.image = [UIImage imageNamed:@"image.png"];
    }
}
使用此基本逻辑可根据您的需要进行自定义。如果数字较大,最好使用
switch case
而不是
if else

- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
    NSArray *listData =[self.tableContents objectForKey:[self.sotreKeys objectAtIndex:[indexPath section]]];


    UITableViewCell * cell = [tableView 
                              dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

    if(cell == nil) {

        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleValue1 
                 reuseIdentifier:SimpleTableIdentifier] autorelease];


    }
    if(indexPath.section == 0 && indexPath.row ==0)
    {
        /*Add image on section of cell*/
cell.imageView.image = [UIImage imageNamed:@"image.png"];
    }
if(indexPath.section == 0 && indexPath.row ==1)
    {
        /*Add image on section of cell*/
cell.imageView.image = [UIImage imageNamed:@"image.png"];
    }
if(indexPath.section == 1 && indexPath.row ==1)
    {
        /*Add image on section of cell*/
cell.imageView.image = [UIImage imageNamed:@"image.png"];
    }
}
用这个我想它会对你有帮助


我想这会对你有所帮助。有很多方法可以做到这一点。如果要将图像存储在数组中,我发现这是最好的方法

将图像存储在一个数组中,这样您就可以使用section和row对其进行索引,并像这样访问它。数组数应为节数。如果要动态更改节数和/或行数,我建议您使用NSMutableArray。如果没有,NSArray就可以正常工作

现在,您可以从诸如cellforrowatinexpath:之类的方法或任何知道单元格的indexPath的方法中设置这些图像,以便使用一行

[[imagesSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
而不是不必要的switch/if语句。我还可以添加一个开关/if,它在为tableview加载单元格时效果不好,但在tableview上使用时非常方便,可以确定哪个tableview需要单元格


如果您需要以不同的方式存储图像,请指定,以便我可以尝试提出更好的解决方案。

有很多方法可以做到这一点。如果要将图像存储在数组中,我发现这是最好的方法

将图像存储在一个数组中,这样您就可以使用section和row对其进行索引,并像这样访问它。数组数应为节数。如果要动态更改节数和/或行数,我建议您使用NSMutableArray。如果没有,NSArray就可以正常工作

现在,您可以从诸如cellforrowatinexpath:之类的方法或任何知道单元格的indexPath的方法中设置这些图像,以便使用一行

[[imagesSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
而不是不必要的switch/if语句。我还可以添加一个开关/if,它在为tableview加载单元格时效果不好,但在tableview上使用时非常方便,可以确定哪个tableview需要单元格

如果您需要以不同的方式存储图像,请指定,以便我可以尝试并提出更好的解决方案