簡體   English   中英

iPhone SDK,在“表格”視圖中顯示NSMutable數組?

[英]IPhone SDK, Display an NSMutable array in Table view?

我試圖通過遵循一個教程在表視圖中顯示我的NSMutableArray。 它由於某種原因而使completley失敗了,我認為我有一個好主意為什么卻無法解決它,這是我的代碼:

- (void) scoreSystem {
scoreArray = [[NSMutableArray alloc] init];
NSNumber *onescore = [NSNumber numberWithInteger:score];
[scoreArray addObject:onescore];
NSNumber *twoscore = [NSNumber numberWithInteger:score];
[scoreArray addObject:twoscore];
NSNumber *threescore = [NSNumber numberWithInteger:score];
[scoreArray addObject:threescore];
NSNumber *fourscore = [NSNumber numberWithInteger:score];
[scoreArray addObject:fourscore];
NSNumber *fivescore = [NSNumber numberWithInteger:score];
[scoreArray addObject:fivescore];
}

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

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];    
}
cell.textLabel.text = [scoreArray objectAtIndex:indexPath.row];

return cell;
}

我認為這是因為它不會讓我在IB中正確鏈接所有內容,而是讓我將數據源放入並委托給文件所有者,但是當我從文件所有者拖到我的視圖時,它說的是'delegate'而不是'視圖”,我認為是因為我在“主窗口”而不是VC中進行操作。 有什么辦法解決嗎? 謝謝! 哈里。

您想要將代碼所在的類設置為tableview的數據源。 在IB中創建您的類的實例(使用NSObject,並將其類重命名為YourClass)。

這將創建您的類的實例,當筆尖被解碼時將可用。

然后,將控件從表視圖拖動到您的類,然后設置數據源。

而已! 您應該能夠在上面的-numberOfRowsInSection:方法中設置斷點,並在表視圖進入視圖后立即看到它被調用。 如果沒有,請檢查連接並檢查錯別字:運行時區分大小寫。

出於某種原因,有人撞了這個舊線程。 我也很樂意。這段代碼有問題的原因是因為它試圖將單元格的text屬性設置為NSNumber

cell.textLabel.text = [scoreArray objectAtIndex:indexPath.row];

嘗試以下方法:

cell.textLabel.text = [[scoreArray objectAtIndex:indexPath.row] stringValue];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM