简体   繁体   English

UITableView:NSMutableArray没有出现在表中

[英]UITableView: NSMutableArray not appearing in table

I'm new to objective c and iPhone programming. 我是目标c和iPhone编程的新手。 I can't seem to figure out why no cells will fill when I run this code. 我似乎无法弄清楚为什么在运行此代码时没有单元格会填满。 The xml content displays in the console log and xcode displays no errors. xml内容显示在控制台日志中,而xcode不显示任何错误。 Can any help? 可以帮忙吗?

    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if(![elementName compare:@"Goal"]) {
        tempElement = [[xmlGoal alloc] init];
    } else if (![elementName compare:@"Title"]) {
        currentAttribute = [NSMutableString string];
    } else if (![elementName compare:@"Progress"]) {
        currentAttribute = [NSMutableString string];
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if (![elementName compare:@"Goal"])  {
        [xmlElementObjects addObject:tempElement];
    } else if (![elementName compare:@"Title"]) {
        NSLog(@"The Title of this event is %@", currentAttribute);
        [tempElement setTitled:currentAttribute];
     } else if (![elementName compare:@"Progress"]) {
        NSLog(@"The Progress of this event is %@", currentAttribute);
        [tempElement setProgressed:currentAttribute];
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (self.currentAttribute) {
        [self.currentAttribute appendString:string];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [xmlElementObjects count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [mtableview dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    // Set up the cell...
    cell.textLabel.text = [xmlElementObjects objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {   

}

I can't seem to figure out why no cells will fill when I run this code. 我似乎无法弄清楚为什么在运行此代码时没有单元格会填满。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [xmlElementObjects count];
}

Is this method getting hit? 这种方法会受到打击吗? If so, is xmlElementObjects nil ? 如果是这样, xmlElementObjects nil If not, is it returning 0 since there are no elements in the array? 如果不是,因为数组中没有元素,它是否返回0? If so, is it because your XML parsing code didn't fill the array before the array is being used? 如果是这样,是因为您的XML解析代码没有在使用数组之前填充数组吗?

Another thing you need to check: Did you set the dataSource property of your tableview to the correct object? 您需要检查的另一件事:是否将表视图的dataSource属性设置为正确的对象? Other than that, check if your if(![elementName compare:@"Goal"]) is really 'hit' (either by NSLog()ing or putting a breakpoint there. 除此之外,检查您的if(![elementName compare:@"Goal"])是否真的“命中”(通过NSLog()ing或在此处放置断点)。

Another note, and unrelated, you should probably release tempElement after adding it to the xmlElements dictionary. 另一个注意事项,与此无关,您可能应该在将tempElement添加到xmlElements字典后释放它。

Set breakpoints in -tableView:numberOfRowsInSection: and -tableView:cellForRowAtIndexPath: to ensure that they're being called. -tableView:numberOfRowsInSection:-tableView:cellForRowAtIndexPath:设置断点,以确保它们被调用。 As Alfons mentioned, it may be that your table view's datasource property is not being set. 如Alfons所述,可能是您的表格视图的datasource属性未设置。

Some unrelated notes: 一些不相​​关的注释:

  1. Why are you using the statement ![someString compare:someOtherString] for comparison? 为什么要使用语句![someString compare:someOtherString]进行比较? NSString has a built-in -isEqualToString: method, so you can replace that statement with [someString isEqaulToString:someOtherString] . NSString具有内置的-isEqualToString:方法,因此您可以将该语句替换为[someString isEqaulToString:someOtherString] Your results won't change, but your code will benefit from the added readability. 您的结果不会改变,但是您的代码将受益于增加的可读性。

  2. In -tableView:cellForRowAtIndexPath: , you have the following code: -tableView:cellForRowAtIndexPath: ,您具有以下代码:

     UITableViewCell *cell = [mtableview dequeueReusableCellWithIdentifier:CellIdentifier]; 

    This might cause problems if you keep using the instance variable (in this case, mtableview ) here. 如果您在此处继续使用实例变量(在本例中为mtableview ),则可能会导致问题。 The method has a table view as an argument, so use tableView instead. 该方法将表视图作为参数,因此请改用tableView That will ensure that you dequeue a cell from the table view that called this method. 这将确保您从调用此方法的表视图中将单元出队。

  3. Are you developing for iPhone OS 3.0 or above? 您是否正在开发iPhone OS 3.0或更高版本? If so, the line 如果是这样,那行

     [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]; 

    should be replaced with 应该替换为

     [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    using whichever cell style you'd like, naturally. 自然使用您想要的任何单元格样式。

Well xmlElementObjects isn't a array of strings, it contains instances to some custom class you've created (I assume). xmlElementObjects不是字符串数组,它包含您创建的某些自定义类的实例(我假设)。 Thus, when you say cell.textLabel.text = [xmlElementObjects objectAtIndex:indexPath.row]; 因此,当您说cell.textLabel.text = [xmlElementObjects objectAtIndex:indexPath.row]; , you're setting a NSString to some unknown object. ,您正在将NSString设置为某些未知对象。

Try cell.textLabel.text = [[xmlElementObjects objectAtIndex:indexPath.row] titled]; 尝试cell.textLabel.text = [[xmlElementObjects objectAtIndex:indexPath.row] titled];

(I'm guessing the title name from the parsing code) (我从解析代码中猜测标题名称)

Objective C is very new to me. 目标C对我来说是非常新的。 I am used to writing PHP which seems very easy compared to this so far. 到目前为止,我已经习惯写PHP了,与之相比,这似乎非常容易。 I set the datasource and also change compare to isequaltostring. 我设置了数据源,还更改了与isequaltostring的比较。 This is what the console log states now: 这是控制台日志现在指出的内容:

2010-04-07 02:28:16.580 bucket list[6449:20b] The Title of this event is Test 1 10% 2010-04-07 02:28:16.580遗愿清单[6449:20b]该活动的标题是Test 1 10%

2010-04-07 02:28:16.581 bucket list[6449:20b] The Title of this event is Test 2 20% 2010-04-07 02:28:16.581遗愿清单[6449:20b]该活动的标题是Test 2 20%

2010-04-07 02:28:16.584 list[6449:20b] * -[xmlGoal isEqualToString:]: unrecognized selector sent to instance 0x3d36600 2010-04-07 02:28:16.585 list[6449:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[xmlGoal isEqualToString:]: unrecognized selector sent to instance 0x3d36600' 2010-04-07 02:28:16.584 list [6449:20b] *-[xmlGoal isEqualToString:]:无法识别的选择器已发送至实例0x3d36600 2010-04-07 02:28:16.585 list [6449:20b] *正在终止应用程序到未捕获的异常“ NSInvalidArgumentException”,原因:“ ***-[xmlGoal isEqualToString:]:无法识别的选择器已发送到实例0x3d36600”

I appreciate all the help! 我感谢所有的帮助!

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

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