This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center




7年前关闭。




我有一个图,里面有很多条目。它通过使用plist的For循环加载数据..表示一个接一个..现在我想要这样一种方式:如果从中读取的数据是greater than 6,则应该采用substringToIndex:2其他substringToIndex:6
但问题是:数据在plist中并一个接一个地获取..我想如果是greater than 6,那么它应该是整个值的substringToIndex:2,而不是下一个值。但是如果大于6则如何重新加载数据以使整个值substringToIndex:2的值

代码
- (void)drawRect:(CGRect)rect {

  int l=0;
  NSMutableArray *Array=[NSMutableArray arrayWithArray:[self readFromPlistForOneWeek]];
  NSMutableArray *items = [[NSMutableArray alloc] init];

  for (id object in [Array reverseObjectEnumerator]){

    if ([object isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *objDict = (NSDictionary *)object;

        //problem part below
           l++;
        if(l>6){
              str1=[str1 substringToIndex:2];
              tempItemi.isPercentage=YES;
              tempItemi.yValue=f;
              tempItemi.width=10;
              tempItemi.name=str1;
              [items addObject: tempItemi];
          } else{
              str1 =[str1 substringToIndex:6];
              tempItemi.isPercentage=YES;
              tempItemi.yValue=f;
              tempItemi.width=10;
              tempItemi.name=str1;
              [items addObject: tempItemi];
          }
     }

这给了我输出




在这里,如果值大于6,则应为所有..而不是大于6(而不是图像中的最后2个)采用subStringToIndex:2。

最佳答案

您不必使用if (l>6)。您可以直接使用,

if ([Array count] > 6)

上面的代码将不起作用,因为您正在循环中对其进行递增,并在递增时进行检查。您需要检查数组的数量。

关于iphone - Objective-C重新加载问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14190017/

10-10 21:10